add isl_pw_aff_mul
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2011      Universiteit Leiden
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9  * 91893 Orsay, France
10  * and Leiden Institute of Advanced Computer Science,
11  * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12  */
13
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl/constraint.h>
20 #include <isl/seq.h>
21 #include <isl/set.h>
22
23 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
24         __isl_take isl_vec *v)
25 {
26         isl_aff *aff;
27
28         if (!ls || !v)
29                 goto error;
30
31         aff = isl_calloc_type(v->ctx, struct isl_aff);
32         if (!aff)
33                 goto error;
34
35         aff->ref = 1;
36         aff->ls = ls;
37         aff->v = v;
38
39         return aff;
40 error:
41         isl_local_space_free(ls);
42         isl_vec_free(v);
43         return NULL;
44 }
45
46 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
47 {
48         isl_ctx *ctx;
49         isl_vec *v;
50         unsigned total;
51
52         if (!ls)
53                 return NULL;
54
55         ctx = isl_local_space_get_ctx(ls);
56         if (!isl_local_space_divs_known(ls))
57                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
58                         goto error);
59
60         total = isl_local_space_dim(ls, isl_dim_all);
61         v = isl_vec_alloc(ctx, 1 + 1 + total);
62         return isl_aff_alloc_vec(ls, v);
63 error:
64         isl_local_space_free(ls);
65         return NULL;
66 }
67
68 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
69 {
70         isl_aff *aff;
71
72         aff = isl_aff_alloc(ls);
73         if (!aff)
74                 return NULL;
75
76         isl_int_set_si(aff->v->el[0], 1);
77         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
78
79         return aff;
80 }
81
82 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
83 {
84         if (!aff)
85                 return NULL;
86
87         aff->ref++;
88         return aff;
89 }
90
91 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
92 {
93         if (!aff)
94                 return NULL;
95
96         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
97                                  isl_vec_copy(aff->v));
98 }
99
100 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
101 {
102         if (!aff)
103                 return NULL;
104
105         if (aff->ref == 1)
106                 return aff;
107         aff->ref--;
108         return isl_aff_dup(aff);
109 }
110
111 void *isl_aff_free(__isl_take isl_aff *aff)
112 {
113         if (!aff)
114                 return NULL;
115
116         if (--aff->ref > 0)
117                 return NULL;
118
119         isl_local_space_free(aff->ls);
120         isl_vec_free(aff->v);
121
122         free(aff);
123
124         return NULL;
125 }
126
127 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
128 {
129         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
130 }
131
132 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
133 {
134         return aff ? isl_local_space_dim(aff->ls, type) : 0;
135 }
136
137 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
138 {
139         return aff ? isl_local_space_get_dim(aff->ls) : NULL;
140 }
141
142 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
143 {
144         return aff ? isl_local_space_copy(aff->ls) : NULL;
145 }
146
147 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
148         enum isl_dim_type type, unsigned pos)
149 {
150         return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
151 }
152
153 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
154         __isl_take isl_dim *dim)
155 {
156         aff = isl_aff_cow(aff);
157         if (!aff || !dim)
158                 goto error;
159
160         aff->ls = isl_local_space_reset_dim(aff->ls, dim);
161         if (!aff->ls)
162                 return isl_aff_free(aff);
163
164         return aff;
165 error:
166         isl_aff_free(aff);
167         isl_dim_free(dim);
168         return NULL;
169 }
170
171 /* Reorder the coefficients of the affine expression based
172  * on the given reodering.
173  * The reordering r is assumed to have been extended with the local
174  * variables.
175  */
176 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
177         __isl_take isl_reordering *r, int n_div)
178 {
179         isl_vec *res;
180         int i;
181
182         if (!vec || !r)
183                 goto error;
184
185         res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
186         isl_seq_cpy(res->el, vec->el, 2);
187         isl_seq_clr(res->el + 2, res->size - 2);
188         for (i = 0; i < r->len; ++i)
189                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
190
191         isl_reordering_free(r);
192         isl_vec_free(vec);
193         return res;
194 error:
195         isl_vec_free(vec);
196         isl_reordering_free(r);
197         return NULL;
198 }
199
200 /* Reorder the dimensions of "aff" according to the given reordering.
201  */
202 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
203         __isl_take isl_reordering *r)
204 {
205         aff = isl_aff_cow(aff);
206         if (!aff)
207                 goto error;
208
209         r = isl_reordering_extend(r, aff->ls->div->n_row);
210         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
211                                 aff->ls->div->n_row);
212         aff->ls = isl_local_space_realign(aff->ls, r);
213
214         if (!aff->v || !aff->ls)
215                 return isl_aff_free(aff);
216
217         return aff;
218 error:
219         isl_aff_free(aff);
220         isl_reordering_free(r);
221         return NULL;
222 }
223
224 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
225 {
226         if (!aff)
227                 return -1;
228
229         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
230 }
231
232 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
233 {
234         int equal;
235
236         if (!aff1 || !aff2)
237                 return -1;
238
239         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
240         if (equal < 0 || !equal)
241                 return equal;
242
243         return isl_vec_is_equal(aff1->v, aff2->v);
244 }
245
246 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
247 {
248         if (!aff)
249                 return -1;
250         isl_int_set(*v, aff->v->el[0]);
251         return 0;
252 }
253
254 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
255 {
256         if (!aff)
257                 return -1;
258         isl_int_set(*v, aff->v->el[1]);
259         return 0;
260 }
261
262 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
263         enum isl_dim_type type, int pos, isl_int *v)
264 {
265         if (!aff)
266                 return -1;
267
268         if (pos >= isl_local_space_dim(aff->ls, type))
269                 isl_die(aff->v->ctx, isl_error_invalid,
270                         "position out of bounds", return -1);
271
272         pos += isl_local_space_offset(aff->ls, type);
273         isl_int_set(*v, aff->v->el[1 + pos]);
274
275         return 0;
276 }
277
278 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
279 {
280         aff = isl_aff_cow(aff);
281         if (!aff)
282                 return NULL;
283
284         aff->v = isl_vec_cow(aff->v);
285         if (!aff->v)
286                 return isl_aff_free(aff);
287
288         isl_int_set(aff->v->el[0], v);
289
290         return aff;
291 }
292
293 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
294 {
295         aff = isl_aff_cow(aff);
296         if (!aff)
297                 return NULL;
298
299         aff->v = isl_vec_cow(aff->v);
300         if (!aff->v)
301                 return isl_aff_free(aff);
302
303         isl_int_set(aff->v->el[1], v);
304
305         return aff;
306 }
307
308 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
309 {
310         if (isl_int_is_zero(v))
311                 return aff;
312
313         aff = isl_aff_cow(aff);
314         if (!aff)
315                 return NULL;
316
317         aff->v = isl_vec_cow(aff->v);
318         if (!aff->v)
319                 return isl_aff_free(aff);
320
321         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
322
323         return aff;
324 }
325
326 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
327 {
328         isl_int t;
329
330         isl_int_init(t);
331         isl_int_set_si(t, v);
332         aff = isl_aff_add_constant(aff, t);
333         isl_int_clear(t);
334
335         return aff;
336 }
337
338 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
339 {
340         aff = isl_aff_cow(aff);
341         if (!aff)
342                 return NULL;
343
344         aff->v = isl_vec_cow(aff->v);
345         if (!aff->v)
346                 return isl_aff_free(aff);
347
348         isl_int_set_si(aff->v->el[1], v);
349
350         return aff;
351 }
352
353 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
354         enum isl_dim_type type, int pos, isl_int v)
355 {
356         if (!aff)
357                 return NULL;
358
359         if (pos >= isl_local_space_dim(aff->ls, type))
360                 isl_die(aff->v->ctx, isl_error_invalid,
361                         "position out of bounds", return isl_aff_free(aff));
362
363         aff = isl_aff_cow(aff);
364         if (!aff)
365                 return NULL;
366
367         aff->v = isl_vec_cow(aff->v);
368         if (!aff->v)
369                 return isl_aff_free(aff);
370
371         pos += isl_local_space_offset(aff->ls, type);
372         isl_int_set(aff->v->el[1 + pos], v);
373
374         return aff;
375 }
376
377 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
378         enum isl_dim_type type, int pos, int v)
379 {
380         if (!aff)
381                 return NULL;
382
383         if (pos >= isl_local_space_dim(aff->ls, type))
384                 isl_die(aff->v->ctx, isl_error_invalid,
385                         "position out of bounds", return isl_aff_free(aff));
386
387         aff = isl_aff_cow(aff);
388         if (!aff)
389                 return NULL;
390
391         aff->v = isl_vec_cow(aff->v);
392         if (!aff->v)
393                 return isl_aff_free(aff);
394
395         pos += isl_local_space_offset(aff->ls, type);
396         isl_int_set_si(aff->v->el[1 + pos], v);
397
398         return aff;
399 }
400
401 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
402         enum isl_dim_type type, int pos, isl_int v)
403 {
404         if (!aff)
405                 return NULL;
406
407         if (pos >= isl_local_space_dim(aff->ls, type))
408                 isl_die(aff->v->ctx, isl_error_invalid,
409                         "position out of bounds", return isl_aff_free(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         pos += isl_local_space_offset(aff->ls, type);
420         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
421
422         return aff;
423 }
424
425 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
426         enum isl_dim_type type, int pos, int v)
427 {
428         isl_int t;
429
430         isl_int_init(t);
431         isl_int_set_si(t, v);
432         aff = isl_aff_add_coefficient(aff, type, pos, t);
433         isl_int_clear(t);
434
435         return aff;
436 }
437
438 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
439 {
440         if (!aff)
441                 return NULL;
442
443         return isl_local_space_get_div(aff->ls, pos);
444 }
445
446 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
447 {
448         aff = isl_aff_cow(aff);
449         if (!aff)
450                 return NULL;
451         aff->v = isl_vec_cow(aff->v);
452         if (!aff->v)
453                 return isl_aff_free(aff);
454
455         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
456
457         return aff;
458 }
459
460 /* Given f, return floor(f).
461  * If f is an integer expression, then just return f.
462  * Otherwise, create a new div d = [f] and return the expression d.
463  */
464 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
465 {
466         int size;
467         isl_ctx *ctx;
468
469         if (!aff)
470                 return NULL;
471
472         if (isl_int_is_one(aff->v->el[0]))
473                 return aff;
474
475         aff = isl_aff_cow(aff);
476         if (!aff)
477                 return NULL;
478
479         aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
480         if (!aff->ls)
481                 return isl_aff_free(aff);
482
483         ctx = isl_aff_get_ctx(aff);
484         size = aff->v->size;
485         isl_vec_free(aff->v);
486         aff->v = isl_vec_alloc(ctx, size + 1);
487         aff->v = isl_vec_clr(aff->v);
488         if (!aff->v)
489                 return isl_aff_free(aff);
490         isl_int_set_si(aff->v->el[0], 1);
491         isl_int_set_si(aff->v->el[size], 1);
492
493         return aff;
494 }
495
496 /* Given f, return ceil(f).
497  * If f is an integer expression, then just return f.
498  * Otherwise, create a new div d = [-f] and return the expression -d.
499  */
500 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
501 {
502         if (!aff)
503                 return NULL;
504
505         if (isl_int_is_one(aff->v->el[0]))
506                 return aff;
507
508         aff = isl_aff_neg(aff);
509         aff = isl_aff_floor(aff);
510         aff = isl_aff_neg(aff);
511
512         return aff;
513 }
514
515 /* Apply the expansion computed by isl_merge_divs.
516  * The expansion itself is given by "exp" while the resulting
517  * list of divs is given by "div".
518  */
519 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
520         __isl_take isl_mat *div, int *exp)
521 {
522         int i, j;
523         int old_n_div;
524         int new_n_div;
525         int offset;
526
527         aff = isl_aff_cow(aff);
528         if (!aff || !div)
529                 goto error;
530
531         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
532         new_n_div = isl_mat_rows(div);
533         if (new_n_div < old_n_div)
534                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
535                         "not an expansion", goto error);
536
537         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
538         if (!aff->v)
539                 goto error;
540
541         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
542         j = old_n_div - 1;
543         for (i = new_n_div - 1; i >= 0; --i) {
544                 if (j >= 0 && exp[j] == i) {
545                         if (i != j)
546                                 isl_int_swap(aff->v->el[offset + i],
547                                              aff->v->el[offset + j]);
548                         j--;
549                 } else
550                         isl_int_set_si(aff->v->el[offset + i], 0);
551         }
552
553         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
554         if (!aff->ls)
555                 goto error;
556         isl_mat_free(div);
557         return aff;
558 error:
559         isl_aff_free(aff);
560         isl_mat_free(div);
561         return NULL;
562 }
563
564 /* Add two affine expressions that live in the same local space.
565  */
566 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
567         __isl_take isl_aff *aff2)
568 {
569         isl_int gcd, f;
570
571         aff1 = isl_aff_cow(aff1);
572         if (!aff1 || !aff2)
573                 goto error;
574
575         aff1->v = isl_vec_cow(aff1->v);
576         if (!aff1->v)
577                 goto error;
578
579         isl_int_init(gcd);
580         isl_int_init(f);
581         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
582         isl_int_divexact(f, aff2->v->el[0], gcd);
583         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
584         isl_int_divexact(f, aff1->v->el[0], gcd);
585         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
586         isl_int_divexact(f, aff2->v->el[0], gcd);
587         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
588         isl_int_clear(f);
589         isl_int_clear(gcd);
590
591         isl_aff_free(aff2);
592         return aff1;
593 error:
594         isl_aff_free(aff1);
595         isl_aff_free(aff2);
596         return NULL;
597 }
598
599 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
600         __isl_take isl_aff *aff2)
601 {
602         isl_ctx *ctx;
603         int *exp1 = NULL;
604         int *exp2 = NULL;
605         isl_mat *div;
606
607         if (!aff1 || !aff2)
608                 goto error;
609
610         ctx = isl_aff_get_ctx(aff1);
611         if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
612                 isl_die(ctx, isl_error_invalid,
613                         "spaces don't match", goto error);
614
615         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
616                 return add_expanded(aff1, aff2);
617
618         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
619         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
620         if (!exp1 || !exp2)
621                 goto error;
622
623         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
624         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
625         aff2 = isl_aff_expand_divs(aff2, div, exp2);
626         free(exp1);
627         free(exp2);
628
629         return add_expanded(aff1, aff2);
630 error:
631         free(exp1);
632         free(exp2);
633         isl_aff_free(aff1);
634         isl_aff_free(aff2);
635         return NULL;
636 }
637
638 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
639         __isl_take isl_aff *aff2)
640 {
641         return isl_aff_add(aff1, isl_aff_neg(aff2));
642 }
643
644 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
645 {
646         isl_int gcd;
647
648         if (isl_int_is_one(f))
649                 return aff;
650
651         aff = isl_aff_cow(aff);
652         if (!aff)
653                 return NULL;
654         aff->v = isl_vec_cow(aff->v);
655         if (!aff->v)
656                 return isl_aff_free(aff);
657
658         isl_int_init(gcd);
659         isl_int_gcd(gcd, aff->v->el[0], f);
660         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
661         isl_int_divexact(gcd, f, gcd);
662         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
663         isl_int_clear(gcd);
664
665         return aff;
666 }
667
668 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
669 {
670         isl_int gcd;
671
672         if (isl_int_is_one(f))
673                 return aff;
674
675         aff = isl_aff_cow(aff);
676         if (!aff)
677                 return NULL;
678         aff->v = isl_vec_cow(aff->v);
679         if (!aff->v)
680                 return isl_aff_free(aff);
681
682         isl_int_init(gcd);
683         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
684         isl_int_gcd(gcd, gcd, f);
685         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
686         isl_int_divexact(gcd, f, gcd);
687         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
688         isl_int_clear(gcd);
689
690         return aff;
691 }
692
693 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
694 {
695         isl_int v;
696
697         if (f == 1)
698                 return aff;
699
700         isl_int_init(v);
701         isl_int_set_ui(v, f);
702         aff = isl_aff_scale_down(aff, v);
703         isl_int_clear(v);
704
705         return aff;
706 }
707
708 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
709         enum isl_dim_type type, unsigned pos, const char *s)
710 {
711         aff = isl_aff_cow(aff);
712         if (!aff)
713                 return NULL;
714         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
715         if (!aff->ls)
716                 return isl_aff_free(aff);
717
718         return aff;
719 }
720
721 /* Exploit the equalities in "eq" to simplify the affine expression
722  * and the expressions of the integer divisions in the local space.
723  * The integer divisions in this local space are assumed to appear
724  * as regular dimensions in "eq".
725  */
726 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
727         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
728 {
729         int i, j;
730         unsigned total;
731         unsigned n_div;
732
733         if (!eq)
734                 goto error;
735         if (eq->n_eq == 0) {
736                 isl_basic_set_free(eq);
737                 return aff;
738         }
739
740         aff = isl_aff_cow(aff);
741         if (!aff)
742                 goto error;
743
744         aff->ls = isl_local_space_substitute_equalities(aff->ls,
745                                                         isl_basic_set_copy(eq));
746         if (!aff->ls)
747                 goto error;
748
749         total = 1 + isl_dim_total(eq->dim);
750         n_div = eq->n_div;
751         for (i = 0; i < eq->n_eq; ++i) {
752                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
753                 if (j < 0 || j == 0 || j >= total)
754                         continue;
755
756                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
757                                 &aff->v->el[0]);
758         }
759
760         isl_basic_set_free(eq);
761         return aff;
762 error:
763         isl_basic_set_free(eq);
764         isl_aff_free(aff);
765         return NULL;
766 }
767
768 /* Exploit the equalities in "eq" to simplify the affine expression
769  * and the expressions of the integer divisions in the local space.
770  */
771 static __isl_give isl_aff *isl_aff_substitute_equalities(
772         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
773 {
774         int n_div;
775
776         if (!aff || !eq)
777                 goto error;
778         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
779         if (n_div > 0)
780                 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
781         return isl_aff_substitute_equalities_lifted(aff, eq);
782 error:
783         isl_basic_set_free(eq);
784         isl_aff_free(aff);
785         return NULL;
786 }
787
788 /* Look for equalities among the variables shared by context and aff
789  * and the integer divisions of aff, if any.
790  * The equalities are then used to eliminate coefficients and/or integer
791  * divisions from aff.
792  */
793 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
794         __isl_take isl_set *context)
795 {
796         isl_basic_set *hull;
797         int n_div;
798
799         if (!aff)
800                 goto error;
801         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
802         if (n_div > 0) {
803                 isl_basic_set *bset;
804                 context = isl_set_add_dims(context, isl_dim_set, n_div);
805                 bset = isl_basic_set_from_local_space(
806                                             isl_aff_get_local_space(aff));
807                 bset = isl_basic_set_lift(bset);
808                 bset = isl_basic_set_flatten(bset);
809                 context = isl_set_intersect(context,
810                                             isl_set_from_basic_set(bset));
811         }
812
813         hull = isl_set_affine_hull(context);
814         return isl_aff_substitute_equalities_lifted(aff, hull);
815 error:
816         isl_aff_free(aff);
817         isl_set_free(context);
818         return NULL;
819 }
820
821 /* Return a basic set containing those elements in the space
822  * of aff where it is non-negative.
823  */
824 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
825 {
826         isl_constraint *ineq;
827
828         ineq = isl_inequality_from_aff(aff);
829
830         return isl_basic_set_from_constraint(ineq);
831 }
832
833 /* Return a basic set containing those elements in the space
834  * of aff where it is zero.
835  */
836 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
837 {
838         isl_constraint *ineq;
839
840         ineq = isl_equality_from_aff(aff);
841
842         return isl_basic_set_from_constraint(ineq);
843 }
844
845 /* Return a basic set containing those elements in the shared space
846  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
847  */
848 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
849         __isl_take isl_aff *aff2)
850 {
851         aff1 = isl_aff_sub(aff1, aff2);
852
853         return isl_aff_nonneg_basic_set(aff1);
854 }
855
856 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
857         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
858 {
859         aff1 = isl_aff_add(aff1, aff2);
860         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
861         return aff1;
862 }
863
864 int isl_aff_is_empty(__isl_keep isl_aff *aff)
865 {
866         if (!aff)
867                 return -1;
868
869         return 0;
870 }
871
872 /* Set active[i] to 1 if the dimension at position i is involved
873  * in the affine expression.
874  */
875 static int set_active(__isl_keep isl_aff *aff, int *active)
876 {
877         int i, j;
878         unsigned total;
879         unsigned offset;
880
881         if (!aff || !active)
882                 return -1;
883
884         total = aff->v->size - 2;
885         for (i = 0; i < total; ++i)
886                 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
887
888         offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
889         for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
890                 if (!active[offset + i])
891                         continue;
892                 for (j = 0; j < total; ++j)
893                         active[j] |=
894                                 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
895         }
896
897         return 0;
898 }
899
900 /* Check whether the given affine expression has non-zero coefficient
901  * for any dimension in the given range or if any of these dimensions
902  * appear with non-zero coefficients in any of the integer divisions
903  * involved in the affine expression.
904  */
905 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
906         enum isl_dim_type type, unsigned first, unsigned n)
907 {
908         int i;
909         isl_ctx *ctx;
910         int *active = NULL;
911         int involves = 0;
912
913         if (!aff)
914                 return -1;
915         if (n == 0)
916                 return 0;
917
918         ctx = isl_aff_get_ctx(aff);
919         if (first + n > isl_aff_dim(aff, type))
920                 isl_die(ctx, isl_error_invalid,
921                         "range out of bounds", return -1);
922
923         active = isl_calloc_array(ctx, int,
924                                   isl_local_space_dim(aff->ls, isl_dim_all));
925         if (set_active(aff, active) < 0)
926                 goto error;
927
928         first += isl_local_space_offset(aff->ls, type) - 1;
929         for (i = 0; i < n; ++i)
930                 if (active[first + i]) {
931                         involves = 1;
932                         break;
933                 }
934
935         free(active);
936
937         return involves;
938 error:
939         free(active);
940         return -1;
941 }
942
943 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
944         enum isl_dim_type type, unsigned first, unsigned n)
945 {
946         isl_ctx *ctx;
947
948         if (!aff)
949                 return NULL;
950         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
951                 return aff;
952
953         ctx = isl_aff_get_ctx(aff);
954         if (first + n > isl_aff_dim(aff, type))
955                 isl_die(ctx, isl_error_invalid, "range out of bounds",
956                         return isl_aff_free(aff));
957
958         aff = isl_aff_cow(aff);
959         if (!aff)
960                 return NULL;
961
962         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
963         if (!aff->ls)
964                 return isl_aff_free(aff);
965
966         first += 1 + isl_local_space_offset(aff->ls, type);
967         aff->v = isl_vec_drop_els(aff->v, first, n);
968         if (!aff->v)
969                 return isl_aff_free(aff);
970
971         return aff;
972 }
973
974 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
975         enum isl_dim_type type, unsigned first, unsigned n)
976 {
977         isl_ctx *ctx;
978
979         if (!aff)
980                 return NULL;
981         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
982                 return aff;
983
984         ctx = isl_aff_get_ctx(aff);
985         if (first > isl_aff_dim(aff, type))
986                 isl_die(ctx, isl_error_invalid, "position out of bounds",
987                         return isl_aff_free(aff));
988
989         aff = isl_aff_cow(aff);
990         if (!aff)
991                 return NULL;
992
993         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
994         if (!aff->ls)
995                 return isl_aff_free(aff);
996
997         first += 1 + isl_local_space_offset(aff->ls, type);
998         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
999         if (!aff->v)
1000                 return isl_aff_free(aff);
1001
1002         return aff;
1003 }
1004
1005 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1006         enum isl_dim_type type, unsigned n)
1007 {
1008         unsigned pos;
1009
1010         pos = isl_aff_dim(aff, type);
1011
1012         return isl_aff_insert_dims(aff, type, pos, n);
1013 }
1014
1015 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1016         enum isl_dim_type type, unsigned n)
1017 {
1018         unsigned pos;
1019
1020         pos = isl_pw_aff_dim(pwaff, type);
1021
1022         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1023 }
1024
1025 #undef PW
1026 #define PW isl_pw_aff
1027 #undef EL
1028 #define EL isl_aff
1029 #undef EL_IS_ZERO
1030 #define EL_IS_ZERO is_empty
1031 #undef ZERO
1032 #define ZERO empty
1033 #undef IS_ZERO
1034 #define IS_ZERO is_empty
1035 #undef FIELD
1036 #define FIELD aff
1037
1038 #define NO_EVAL
1039 #define NO_OPT
1040 #define NO_MOVE_DIMS
1041 #define NO_LIFT
1042 #define NO_MORPH
1043
1044 #include <isl_pw_templ.c>
1045
1046 /* Compute a piecewise quasi-affine expression with a domain that
1047  * is the union of those of pwaff1 and pwaff2 and such that on each
1048  * cell, the quasi-affine expression is the maximum of those of pwaff1
1049  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1050  * cell, then the associated expression is the defined one.
1051  */
1052 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1053         __isl_take isl_pw_aff *pwaff2)
1054 {
1055         int i, j, n;
1056         isl_pw_aff *res;
1057         isl_ctx *ctx;
1058         isl_set *set;
1059
1060         if (!pwaff1 || !pwaff2)
1061                 goto error;
1062
1063         ctx = isl_dim_get_ctx(pwaff1->dim);
1064         if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1065                 isl_die(ctx, isl_error_invalid,
1066                         "arguments should live in same space", goto error);
1067
1068         if (isl_pw_aff_is_empty(pwaff1)) {
1069                 isl_pw_aff_free(pwaff1);
1070                 return pwaff2;
1071         }
1072
1073         if (isl_pw_aff_is_empty(pwaff2)) {
1074                 isl_pw_aff_free(pwaff2);
1075                 return pwaff1;
1076         }
1077
1078         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1079         res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1080
1081         for (i = 0; i < pwaff1->n; ++i) {
1082                 set = isl_set_copy(pwaff1->p[i].set);
1083                 for (j = 0; j < pwaff2->n; ++j) {
1084                         struct isl_set *common;
1085                         isl_set *ge;
1086
1087                         common = isl_set_intersect(
1088                                         isl_set_copy(pwaff1->p[i].set),
1089                                         isl_set_copy(pwaff2->p[j].set));
1090                         ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1091                                         isl_aff_copy(pwaff2->p[j].aff),
1092                                         isl_aff_copy(pwaff1->p[i].aff)));
1093                         ge = isl_set_intersect(common, ge);
1094                         if (isl_set_plain_is_empty(ge)) {
1095                                 isl_set_free(ge);
1096                                 continue;
1097                         }
1098                         set = isl_set_subtract(set, isl_set_copy(ge));
1099
1100                         res = isl_pw_aff_add_piece(res, ge,
1101                                                 isl_aff_copy(pwaff2->p[j].aff));
1102                 }
1103                 res = isl_pw_aff_add_piece(res, set,
1104                                                 isl_aff_copy(pwaff1->p[i].aff));
1105         }
1106
1107         for (j = 0; j < pwaff2->n; ++j) {
1108                 set = isl_set_copy(pwaff2->p[j].set);
1109                 for (i = 0; i < pwaff1->n; ++i)
1110                         set = isl_set_subtract(set,
1111                                         isl_set_copy(pwaff1->p[i].set));
1112                 res = isl_pw_aff_add_piece(res, set,
1113                                                 isl_aff_copy(pwaff2->p[j].aff));
1114         }
1115
1116         isl_pw_aff_free(pwaff1);
1117         isl_pw_aff_free(pwaff2);
1118
1119         return res;
1120 error:
1121         isl_pw_aff_free(pwaff1);
1122         isl_pw_aff_free(pwaff2);
1123         return NULL;
1124 }
1125
1126 /* Construct a map with as domain the domain of pwaff and
1127  * one-dimensional range corresponding to the affine expressions.
1128  */
1129 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1130 {
1131         int i;
1132         isl_dim *dim;
1133         isl_map *map;
1134
1135         if (!pwaff)
1136                 return NULL;
1137
1138         dim = isl_pw_aff_get_dim(pwaff);
1139         dim = isl_dim_from_domain(dim);
1140         dim = isl_dim_add(dim, isl_dim_out, 1);
1141         map = isl_map_empty(dim);
1142
1143         for (i = 0; i < pwaff->n; ++i) {
1144                 isl_basic_map *bmap;
1145                 isl_map *map_i;
1146
1147                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1148                 map_i = isl_map_from_basic_map(bmap);
1149                 map_i = isl_map_intersect_domain(map_i,
1150                                                 isl_set_copy(pwaff->p[i].set));
1151                 map = isl_map_union_disjoint(map, map_i);
1152         }
1153
1154         isl_pw_aff_free(pwaff);
1155
1156         return map;
1157 }
1158
1159 /* Return a set containing those elements in the domain
1160  * of pwaff where it is non-negative.
1161  */
1162 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1163 {
1164         int i;
1165         isl_set *set;
1166
1167         if (!pwaff)
1168                 return NULL;
1169
1170         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1171
1172         for (i = 0; i < pwaff->n; ++i) {
1173                 isl_basic_set *bset;
1174                 isl_set *set_i;
1175
1176                 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1177                 set_i = isl_set_from_basic_set(bset);
1178                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1179                 set = isl_set_union_disjoint(set, set_i);
1180         }
1181
1182         isl_pw_aff_free(pwaff);
1183
1184         return set;
1185 }
1186
1187 /* Return a set containing those elements in the domain
1188  * of pwaff where it is zero.
1189  */
1190 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1191 {
1192         int i;
1193         isl_set *set;
1194
1195         if (!pwaff)
1196                 return NULL;
1197
1198         set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1199
1200         for (i = 0; i < pwaff->n; ++i) {
1201                 isl_basic_set *bset;
1202                 isl_set *set_i;
1203
1204                 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1205                 set_i = isl_set_from_basic_set(bset);
1206                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1207                 set = isl_set_union_disjoint(set, set_i);
1208         }
1209
1210         isl_pw_aff_free(pwaff);
1211
1212         return set;
1213 }
1214
1215 /* Return a set containing those elements in the shared domain
1216  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1217  *
1218  * We compute the difference on the shared domain and then construct
1219  * the set of values where this difference is non-negative.
1220  * If strict is set, we first subtract 1 from the difference.
1221  * If equal is set, we only return the elements where pwaff1 and pwaff2
1222  * are equal.
1223  */
1224 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1225         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1226 {
1227         isl_set *set1, *set2;
1228
1229         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1230         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1231         set1 = isl_set_intersect(set1, set2);
1232         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1233         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1234         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1235
1236         if (strict) {
1237                 isl_dim *dim = isl_set_get_dim(set1);
1238                 isl_aff *aff;
1239                 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1240                 aff = isl_aff_add_constant_si(aff, -1);
1241                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1242         } else
1243                 isl_set_free(set1);
1244
1245         if (equal)
1246                 return isl_pw_aff_zero_set(pwaff1);
1247         return isl_pw_aff_nonneg_set(pwaff1);
1248 }
1249
1250 /* Return a set containing those elements in the shared domain
1251  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1252  */
1253 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1254         __isl_take isl_pw_aff *pwaff2)
1255 {
1256         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1257 }
1258
1259 /* Return a set containing those elements in the shared domain
1260  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1261  */
1262 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1263         __isl_take isl_pw_aff *pwaff2)
1264 {
1265         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1266 }
1267
1268 /* Return a set containing those elements in the shared domain
1269  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1270  */
1271 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1272         __isl_take isl_pw_aff *pwaff2)
1273 {
1274         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1275 }
1276
1277 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1278         __isl_take isl_pw_aff *pwaff2)
1279 {
1280         return isl_pw_aff_ge_set(pwaff2, pwaff1);
1281 }
1282
1283 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1284         __isl_take isl_pw_aff *pwaff2)
1285 {
1286         return isl_pw_aff_gt_set(pwaff2, pwaff1);
1287 }
1288
1289 /* Return a set containing those elements in the shared domain
1290  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1291  */
1292 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1293         __isl_take isl_pw_aff *pwaff2)
1294 {
1295         isl_set *set_lt, *set_gt;
1296
1297         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1298                                    isl_pw_aff_copy(pwaff2));
1299         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1300         return isl_set_union_disjoint(set_lt, set_gt);
1301 }
1302
1303 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1304         isl_int v)
1305 {
1306         int i;
1307
1308         if (isl_int_is_one(v))
1309                 return pwaff;
1310         if (!isl_int_is_pos(v))
1311                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1312                         "factor needs to be positive",
1313                         return isl_pw_aff_free(pwaff));
1314         pwaff = isl_pw_aff_cow(pwaff);
1315         if (!pwaff)
1316                 return NULL;
1317         if (pwaff->n == 0)
1318                 return pwaff;
1319
1320         for (i = 0; i < pwaff->n; ++i) {
1321                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1322                 if (!pwaff->p[i].aff)
1323                         return isl_pw_aff_free(pwaff);
1324         }
1325
1326         return pwaff;
1327 }
1328
1329 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1330 {
1331         int i;
1332
1333         pwaff = isl_pw_aff_cow(pwaff);
1334         if (!pwaff)
1335                 return NULL;
1336         if (pwaff->n == 0)
1337                 return pwaff;
1338
1339         for (i = 0; i < pwaff->n; ++i) {
1340                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1341                 if (!pwaff->p[i].aff)
1342                         return isl_pw_aff_free(pwaff);
1343         }
1344
1345         return pwaff;
1346 }
1347
1348 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1349 {
1350         int i;
1351
1352         pwaff = isl_pw_aff_cow(pwaff);
1353         if (!pwaff)
1354                 return NULL;
1355         if (pwaff->n == 0)
1356                 return pwaff;
1357
1358         for (i = 0; i < pwaff->n; ++i) {
1359                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1360                 if (!pwaff->p[i].aff)
1361                         return isl_pw_aff_free(pwaff);
1362         }
1363
1364         return pwaff;
1365 }
1366
1367 /* Return an affine expression that is equal to pwaff_true for elements
1368  * in "cond" and to pwaff_false for elements not in "cond".
1369  * That is, return cond ? pwaff_true : pwaff_false;
1370  */
1371 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1372         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1373 {
1374         isl_set *comp;
1375
1376         comp = isl_set_complement(isl_set_copy(cond));
1377         pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1378         pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1379
1380         return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1381 }
1382
1383 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1384 {
1385         if (!aff)
1386                 return -1;
1387
1388         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1389 }
1390
1391 /* Check whether pwaff is a piecewise constant.
1392  */
1393 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1394 {
1395         int i;
1396
1397         if (!pwaff)
1398                 return -1;
1399
1400         for (i = 0; i < pwaff->n; ++i) {
1401                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1402                 if (is_cst < 0 || !is_cst)
1403                         return is_cst;
1404         }
1405
1406         return 1;
1407 }
1408
1409 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1410         __isl_take isl_aff *aff2)
1411 {
1412         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1413                 return isl_aff_mul(aff2, aff1);
1414
1415         if (!isl_aff_is_cst(aff2))
1416                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1417                         "at least one affine expression should be constant",
1418                         goto error);
1419
1420         aff1 = isl_aff_cow(aff1);
1421         if (!aff1 || !aff2)
1422                 goto error;
1423
1424         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1425         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1426
1427         isl_aff_free(aff2);
1428         return aff1;
1429 error:
1430         isl_aff_free(aff1);
1431         isl_aff_free(aff2);
1432         return NULL;
1433 }
1434
1435 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1436         __isl_take isl_pw_aff *pwaff2)
1437 {
1438         int i, j, n;
1439         isl_pw_aff *res;
1440
1441         if (!pwaff1 || !pwaff2)
1442                 goto error;
1443
1444         n = pwaff1->n * pwaff2->n;
1445         res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1446
1447         for (i = 0; i < pwaff1->n; ++i) {
1448                 for (j = 0; j < pwaff2->n; ++j) {
1449                         isl_set *common;
1450                         isl_aff *prod;
1451                         common = isl_set_intersect(
1452                                         isl_set_copy(pwaff1->p[i].set),
1453                                         isl_set_copy(pwaff2->p[j].set));
1454                         if (isl_set_plain_is_empty(common)) {
1455                                 isl_set_free(common);
1456                                 continue;
1457                         }
1458
1459                         prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1460                                             isl_aff_copy(pwaff2->p[j].aff));
1461
1462                         res = isl_pw_aff_add_piece(res, common, prod);
1463                 }
1464         }
1465
1466         isl_pw_aff_free(pwaff1);
1467         isl_pw_aff_free(pwaff2);
1468         return res;
1469 error:
1470         isl_pw_aff_free(pwaff1);
1471         isl_pw_aff_free(pwaff2);
1472         return NULL;
1473 }