b176693944ed76c71ebd24e1392a4d543f8a0c85
[platform/upstream/isl.git] / isl_pw_templ.c
1 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
2 #define FN(TYPE,NAME) xFN(TYPE,NAME)
3 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
4 #define S(TYPE,NAME) xS(TYPE,NAME)
5
6 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
7 {
8         struct PW *pw;
9
10         if (!dim)
11                 return NULL;
12         isl_assert(dim->ctx, n >= 0, goto error);
13         pw = isl_alloc(dim->ctx, struct PW,
14                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
15         if (!pw)
16                 goto error;
17
18         pw->ref = 1;
19         pw->size = n;
20         pw->n = 0;
21         pw->dim = dim;
22         return pw;
23 error:
24         isl_dim_free(dim);
25         return NULL;
26 }
27
28 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
29 {
30         return FN(PW,alloc_)(dim, 0);
31 }
32
33 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
34         __isl_take isl_set *set, __isl_take EL *el)
35 {
36         if (!pw || !set || !el)
37                 goto error;
38
39         if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
40                 isl_set_free(set);
41                 FN(EL,free)(el);
42                 return pw;
43         }
44
45         isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
46         isl_assert(set->ctx, pw->n < pw->size, goto error);
47
48         pw->p[pw->n].set = set;
49         pw->p[pw->n].FIELD = el;
50         pw->n++;
51         
52         return pw;
53 error:
54         FN(PW,free)(pw);
55         isl_set_free(set);
56         FN(EL,free)(el);
57         return NULL;
58 }
59
60 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
61 {
62         PW *pw;
63
64         if (!set || !el)
65                 goto error;
66
67         pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
68
69         return FN(PW,add_piece)(pw, set, el);
70 error:
71         isl_set_free(set);
72         FN(EL,free)(el);
73         return NULL;
74 }
75
76 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
77 {
78         int i;
79         PW *dup;
80
81         if (!pw)
82                 return NULL;
83
84         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
85         if (!dup)
86                 return NULL;
87
88         for (i = 0; i < pw->n; ++i)
89                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
90                                             FN(EL,copy)(pw->p[i].FIELD));
91
92         return dup;
93 error:
94         FN(PW,free)(dup);
95         return NULL;
96 }
97
98 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
99 {
100         if (!pw)
101                 return NULL;
102
103         if (pw->ref == 1)
104                 return pw;
105         pw->ref--;
106         return FN(PW,dup)(pw);
107 }
108
109 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
110 {
111         if (!pw)
112                 return NULL;
113
114         pw->ref++;
115         return pw;
116 }
117
118 void FN(PW,free)(__isl_take PW *pw)
119 {
120         int i;
121
122         if (!pw)
123                 return;
124         if (--pw->ref > 0)
125                 return;
126
127         for (i = 0; i < pw->n; ++i) {
128                 isl_set_free(pw->p[i].set);
129                 FN(EL,free)(pw->p[i].FIELD);
130         }
131         isl_dim_free(pw->dim);
132         free(pw);
133 }
134
135 int FN(PW,is_zero)(__isl_keep PW *pw)
136 {
137         if (!pw)
138                 return -1;
139
140         return pw->n == 0;
141 }
142
143 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
144 {
145         int i, j, n;
146         struct PW *res;
147         isl_set *set;
148
149         if (!pw1 || !pw2)
150                 goto error;
151
152         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
153
154         if (FN(PW,is_zero)(pw1)) {
155                 FN(PW,free)(pw1);
156                 return pw2;
157         }
158
159         if (FN(PW,is_zero)(pw2)) {
160                 FN(PW,free)(pw2);
161                 return pw1;
162         }
163
164         n = (pw1->n + 1) * (pw2->n + 1);
165         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
166
167         for (i = 0; i < pw1->n; ++i) {
168                 set = isl_set_copy(pw1->p[i].set);
169                 for (j = 0; j < pw2->n; ++j) {
170                         struct isl_set *common;
171                         EL *sum;
172                         set = isl_set_subtract(set,
173                                         isl_set_copy(pw2->p[j].set));
174                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
175                                                 isl_set_copy(pw2->p[j].set));
176                         if (isl_set_fast_is_empty(common)) {
177                                 isl_set_free(common);
178                                 continue;
179                         }
180
181                         sum = FN(EL,add_on_domain)(common,
182                                                    FN(EL,copy)(pw1->p[i].FIELD),
183                                                    FN(EL,copy)(pw2->p[j].FIELD));
184
185                         res = FN(PW,add_piece)(res, common, sum);
186                 }
187                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
188         }
189
190         for (j = 0; j < pw2->n; ++j) {
191                 set = isl_set_copy(pw2->p[j].set);
192                 for (i = 0; i < pw1->n; ++i)
193                         set = isl_set_subtract(set,
194                                         isl_set_copy(pw1->p[i].set));
195                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
196         }
197
198         FN(PW,free)(pw1);
199         FN(PW,free)(pw2);
200
201         return res;
202 error:
203         FN(PW,free)(pw1);
204         FN(PW,free)(pw2);
205         return NULL;
206 }
207
208 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
209 {
210         int i;
211         PW *res;
212
213         if (!pw1 || !pw2)
214                 goto error;
215
216         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
217
218         if (FN(PW,is_zero)(pw1)) {
219                 FN(PW,free)(pw1);
220                 return pw2;
221         }
222
223         if (FN(PW,is_zero)(pw2)) {
224                 FN(PW,free)(pw2);
225                 return pw1;
226         }
227
228         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
229
230         for (i = 0; i < pw1->n; ++i)
231                 res = FN(PW,add_piece)(res,
232                                 isl_set_copy(pw1->p[i].set),
233                                 FN(EL,copy)(pw1->p[i].FIELD));
234
235         for (i = 0; i < pw2->n; ++i)
236                 res = FN(PW,add_piece)(res,
237                                 isl_set_copy(pw2->p[i].set),
238                                 FN(EL,copy)(pw2->p[i].FIELD));
239
240         FN(PW,free)(pw1);
241         FN(PW,free)(pw2);
242
243         return res;
244 error:
245         FN(PW,free)(pw1);
246         FN(PW,free)(pw2);
247         return NULL;
248 }
249
250 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
251         __isl_take isl_point *pnt)
252 {
253         int i;
254         int found = 0;
255         isl_qpolynomial *qp;
256
257         if (!pw || !pnt)
258                 goto error;
259         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
260
261         for (i = 0; i < pw->n; ++i) {
262                 found = isl_set_contains_point(pw->p[i].set, pnt);
263                 if (found < 0)
264                         goto error;
265                 if (found)
266                         break;
267         }
268         if (found)
269                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
270                                             isl_point_copy(pnt));
271         else
272                 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
273         FN(PW,free)(pw);
274         isl_point_free(pnt);
275         return qp;
276 error:
277         FN(PW,free)(pw);
278         isl_point_free(pnt);
279         return NULL;
280 }
281
282 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
283 {
284         int i;
285         isl_set *dom;
286
287         if (!pw)
288                 return NULL;
289
290         dom = isl_set_empty(isl_dim_copy(pw->dim));
291         for (i = 0; i < pw->n; ++i)
292                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
293
294         FN(PW,free)(pw);
295
296         return dom;
297 }
298
299 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
300 {
301         int i;
302
303         if (!pw || !set)
304                 goto error;
305
306         if (pw->n == 0) {
307                 isl_set_free(set);
308                 return pw;
309         }
310
311         pw = FN(PW,cow)(pw);
312         if (!pw)
313                 goto error;
314
315         for (i = pw->n - 1; i >= 0; --i) {
316                 isl_basic_set *aff;
317                 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
318                 if (!pw->p[i].set)
319                         goto error;
320                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
321                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
322                                                                 aff);
323                 if (isl_set_fast_is_empty(pw->p[i].set)) {
324                         isl_set_free(pw->p[i].set);
325                         FN(EL,free)(pw->p[i].FIELD);
326                         if (i != pw->n - 1)
327                                 pw->p[i] = pw->p[pw->n - 1];
328                         pw->n--;
329                 }
330         }
331         
332         isl_set_free(set);
333         return pw;
334 error:
335         isl_set_free(set);
336         FN(PW,free)(pw);
337         return NULL;
338 }
339
340 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
341 {
342         int i;
343         isl_basic_set *hull = NULL;
344
345         if (!pw || !context)
346                 goto error;
347
348         if (pw->n == 0) {
349                 isl_set_free(context);
350                 return pw;
351         }
352
353         hull = isl_set_simple_hull(isl_set_copy(context));
354
355         pw = FN(PW,cow)(pw);
356         if (!pw)
357                 goto error;
358
359         for (i = 0; i < pw->n; ++i) {
360                 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
361                                                 isl_basic_set_copy(hull));
362                 if (!pw->p[i].set)
363                         goto error;
364         }
365
366         isl_basic_set_free(hull);
367         isl_set_free(context);
368
369         return pw;
370 error:
371         FN(PW,free)(pw);
372         isl_basic_set_free(hull);
373         isl_set_free(context);
374         return NULL;
375 }
376
377 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
378 {
379         int i, j;
380
381         if (!pw)
382                 return NULL;
383         if (pw->n == 0)
384                 return pw;
385
386         for (i = pw->n - 1; i >= 0; --i) {
387                 for (j = i - 1; j >= 0; --j) {
388                         if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
389                                 continue;
390                         pw->p[j].set = isl_set_union(pw->p[j].set,
391                                                         pw->p[i].set);
392                         FN(EL,free)(pw->p[i].FIELD);
393                         if (i != pw->n - 1)
394                                 pw->p[i] = pw->p[pw->n - 1];
395                         pw->n--;
396                         break;
397                 }
398                 if (j >= 0)
399                         continue;
400                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
401                 if (!pw->p[i].set)
402                         goto error;
403         }
404
405         return pw;
406 error:
407         FN(PW,free)(pw);
408         return NULL;
409 }
410
411 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
412 {
413         return pw ? pw->dim->ctx : NULL;
414 }
415
416 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
417         unsigned first, unsigned n)
418 {
419         int i;
420
421         if (!pw)
422                 return -1;
423         if (pw->n == 0 || n == 0)
424                 return 0;
425         for (i = 0; i < pw->n; ++i) {
426                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
427                                                         type, first, n);
428                 if (involves < 0 || involves)
429                         return involves;
430         }
431         return 0;
432 }
433
434 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
435         enum isl_dim_type type, unsigned first, unsigned n)
436 {
437         int i;
438
439         if (!pw)
440                 return NULL;
441         if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
442                 return pw;
443
444         pw = FN(PW,cow)(pw);
445         if (!pw)
446                 return NULL;
447         pw->dim = isl_dim_drop(pw->dim, type, first, n);
448         if (!pw->dim)
449                 goto error;
450         for (i = 0; i < pw->n; ++i) {
451                 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
452                 if (!pw->p[i].set)
453                         goto error;
454                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
455                 if (!pw->p[i].FIELD)
456                         goto error;
457         }
458
459         return pw;
460 error:
461         FN(PW,free)(pw);
462         return NULL;
463 }
464
465 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
466         enum isl_dim_type type, unsigned pos, isl_int v)
467 {
468         int i;
469
470         if (!pw)
471                 return NULL;
472
473         pw = FN(PW,cow)(pw);
474         if (!pw)
475                 return NULL;
476         for (i = 0; i < pw->n; ++i) {
477                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
478                 if (!pw->p[i].set)
479                         goto error;
480         }
481
482         return pw;
483 error:
484         FN(PW,free)(pw);
485         return NULL;
486 }
487
488 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
489 {
490         return pw ? isl_dim_size(pw->dim, type) : 0;
491 }
492
493 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
494         enum isl_dim_type type, unsigned first, unsigned n)
495 {
496         int i;
497
498         if (!pw)
499                 return NULL;
500         if (n == 0)
501                 return pw;
502
503         pw = FN(PW,cow)(pw);
504         if (!pw)
505                 return NULL;
506         if (!pw->dim)
507                 goto error;
508         for (i = 0; i < pw->n; ++i) {
509                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
510                 if (!pw->p[i].set)
511                         goto error;
512         }
513
514         return pw;
515 error:
516         FN(PW,free)(pw);
517         return NULL;
518 }
519
520 /* Compute the maximal value attained by the piecewise quasipolynomial
521  * on its domain or zero if the domain is empty.
522  * In the worst case, the domain is scanned completely,
523  * so the domain is assumed to be bounded.
524  */
525 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
526 {
527         int i;
528         isl_qpolynomial *opt;
529
530         if (!pw)
531                 return NULL;
532
533         if (pw->n == 0) {
534                 isl_dim *dim = isl_dim_copy(pw->dim);
535                 FN(PW,free)(pw);
536                 return isl_qpolynomial_zero(dim);
537         }
538
539         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
540                                         isl_set_copy(pw->p[0].set), max);
541         for (i = 1; i < pw->n; ++i) {
542                 isl_qpolynomial *opt_i;
543                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
544                                                 isl_set_copy(pw->p[i].set), max);
545                 if (max)
546                         opt = isl_qpolynomial_max_cst(opt, opt_i);
547                 else
548                         opt = isl_qpolynomial_min_cst(opt, opt_i);
549         }
550
551         FN(PW,free)(pw);
552         return opt;
553 }
554
555 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
556 {
557         return FN(PW,opt)(pw, 1);
558 }
559
560 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
561 {
562         return FN(PW,opt)(pw, 0);
563 }
564
565 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
566 {
567         return pw ? isl_dim_copy(pw->dim) : NULL;
568 }
569
570 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
571 {
572         int i;
573
574         pw = FN(PW,cow)(pw);
575         if (!pw || !dim)
576                 goto error;
577
578         for (i = 0; i < pw->n; ++i) {
579                 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
580                                                  isl_dim_copy(dim));
581                 if (!pw->p[i].set)
582                         goto error;
583                 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
584                                                   isl_dim_copy(dim));
585                 if (!pw->p[i].FIELD)
586                         goto error;
587         }
588         isl_dim_free(pw->dim);
589         pw->dim = dim;
590
591         return pw;
592 error:
593         isl_dim_free(dim);
594         FN(PW,free)(pw);
595         return NULL;
596 }
597
598 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
599 {
600         if (!pw1 || !pw2)
601                 return -1;
602
603         return isl_dim_equal(pw1->dim, pw2->dim);
604 }
605
606 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
607 {
608         int i;
609
610         if (!pw || !morph)
611                 goto error;
612
613         isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
614                 goto error);
615
616         pw = FN(PW,cow)(pw);
617         if (!pw)
618                 goto error;
619         isl_dim_free(pw->dim);
620         pw->dim = isl_dim_copy(morph->ran->dim);
621         if (!pw->dim)
622                 goto error;
623
624         for (i = 0; i < pw->n; ++i) {
625                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
626                 if (!pw->p[i].set)
627                         goto error;
628                 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
629                                                 isl_morph_copy(morph));
630                 if (!pw->p[i].FIELD)
631                         goto error;
632         }
633
634         isl_morph_free(morph);
635
636         return pw;
637 error:
638         FN(PW,free)(pw);
639         isl_morph_free(morph);
640         return NULL;
641 }
642
643 int FN(PW,foreach_piece)(__isl_keep PW *pw,
644         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
645         void *user)
646 {
647         int i;
648
649         if (!pw)
650                 return -1;
651
652         for (i = 0; i < pw->n; ++i)
653                 if (fn(isl_set_copy(pw->p[i].set),
654                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
655                         return -1;
656
657         return 0;
658 }
659
660 static int any_divs(__isl_keep isl_set *set)
661 {
662         int i;
663
664         if (!set)
665                 return -1;
666
667         for (i = 0; i < set->n; ++i)
668                 if (set->p[i]->n_div > 0)
669                         return 1;
670
671         return 0;
672 }
673
674 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
675         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
676                     void *user), void *user)
677 {
678         int i;
679
680         if (!set || !el)
681                 goto error;
682
683         for (i = 0; i < set->n; ++i) {
684                 isl_set *lift;
685                 EL *copy;
686
687                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
688                 lift = isl_set_lift(lift);
689
690                 copy = FN(EL,copy)(el);
691                 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
692
693                 if (fn(lift, copy, user) < 0)
694                         goto error;
695         }
696
697         isl_set_free(set);
698         FN(EL,free)(el);
699
700         return 0;
701 error:
702         isl_set_free(set);
703         FN(EL,free)(el);
704         return -1;
705 }
706
707 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
708         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
709                     void *user), void *user)
710 {
711         int i;
712
713         if (!pw)
714                 return -1;
715
716         for (i = 0; i < pw->n; ++i) {
717                 isl_set *set;
718                 EL *el;
719
720                 set = isl_set_copy(pw->p[i].set);
721                 el = FN(EL,copy)(pw->p[i].FIELD);
722                 if (!any_divs(set)) {
723                         if (fn(set, el, user) < 0)
724                                 return -1;
725                         continue;
726                 }
727                 if (foreach_lifted_subset(set, el, fn, user) < 0)
728                         return -1;
729         }
730
731         return 0;
732 }
733
734 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
735         enum isl_dim_type dst_type, unsigned dst_pos,
736         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
737 {
738         int i;
739
740         pw = FN(PW,cow)(pw);
741         if (!pw)
742                 return NULL;
743
744         pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
745         if (!pw->dim)
746                 goto error;
747
748         for (i = 0; i < pw->n; ++i) {
749                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
750                                                 dst_type, dst_pos,
751                                                 src_type, src_pos, n);
752                 if (!pw->p[i].set)
753                         goto error;
754                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
755                                         dst_type, dst_pos, src_type, src_pos, n);
756                 if (!pw->p[i].FIELD)
757                         goto error;
758         }
759
760         return pw;
761 error:
762         FN(PW,free)(pw);
763         return NULL;
764 }