test both context handling mechanisms of isl_pip
[platform/upstream/isl.git] / isl_pw_templ.c
index 30b5dfa..0555091 100644 (file)
@@ -3,7 +3,12 @@
 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
 #define S(TYPE,NAME) xS(TYPE,NAME)
 
+#ifdef HAS_TYPE
+static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim,
+       enum isl_fold type, int n)
+#else
 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
+#endif
 {
        struct PW *pw;
 
@@ -16,6 +21,9 @@ static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
                goto error;
 
        pw->ref = 1;
+#ifdef HAS_TYPE
+       pw->type = type;
+#endif
        pw->size = n;
        pw->n = 0;
        pw->dim = dim;
@@ -25,10 +33,17 @@ error:
        return NULL;
 }
 
+#ifdef HAS_TYPE
+__isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
+{
+       return FN(PW,alloc_)(dim, type, 0);
+}
+#else
 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
 {
        return FN(PW,alloc_)(dim, 0);
 }
+#endif
 
 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
        __isl_take isl_set *set, __isl_take EL *el)
@@ -42,6 +57,11 @@ __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
                return pw;
        }
 
+#ifdef HAS_TYPE
+       if (pw->type != el->type)
+               isl_die(set->ctx, isl_error_invalid, "fold types don't match",
+                       goto error);
+#endif
        isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
        isl_assert(set->ctx, pw->n < pw->size, goto error);
 
@@ -57,14 +77,23 @@ error:
        return NULL;
 }
 
+#ifdef HAS_TYPE
+__isl_give PW *FN(PW,alloc)(enum isl_fold type,
+       __isl_take isl_set *set, __isl_take EL *el)
+#else
 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
+#endif
 {
        PW *pw;
 
        if (!set || !el)
                goto error;
 
+#ifdef HAS_TYPE
+       pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
+#else
        pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
+#endif
 
        return FN(PW,add_piece)(pw, set, el);
 error:
@@ -81,7 +110,11 @@ __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
        if (!pw)
                return NULL;
 
+#ifdef HAS_TYPE
+       dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
+#else
        dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
+#endif
        if (!dup)
                return NULL;
 
@@ -149,6 +182,11 @@ __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
        if (!pw1 || !pw2)
                goto error;
 
+#ifdef HAS_TYPE
+       if (pw1->type != pw2->type)
+               isl_die(pw1->dim->ctx, isl_error_invalid,
+                       "fold types don't match", goto error);
+#endif
        isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
 
        if (FN(PW,is_zero)(pw1)) {
@@ -162,7 +200,11 @@ __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
        }
 
        n = (pw1->n + 1) * (pw2->n + 1);
+#ifdef HAS_TYPE
+       res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
+#else
        res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
+#endif
 
        for (i = 0; i < pw1->n; ++i) {
                set = isl_set_copy(pw1->p[i].set);
@@ -178,8 +220,9 @@ __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
                                continue;
                        }
 
-                       sum = ADD(common, FN(EL,copy)(pw1->p[i].FIELD),
-                                         FN(EL,copy)(pw2->p[j].FIELD));
+                       sum = FN(EL,add_on_domain)(common,
+                                                  FN(EL,copy)(pw1->p[i].FIELD),
+                                                  FN(EL,copy)(pw2->p[j].FIELD));
 
                        res = FN(PW,add_piece)(res, common, sum);
                }
@@ -212,6 +255,11 @@ __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
        if (!pw1 || !pw2)
                goto error;
 
+#ifdef HAS_TYPE
+       if (pw1->type != pw2->type)
+               isl_die(pw1->dim->ctx, isl_error_invalid,
+                       "fold types don't match", goto error);
+#endif
        isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
 
        if (FN(PW,is_zero)(pw1)) {
@@ -224,7 +272,11 @@ __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
                return pw1;
        }
 
+#ifdef HAS_TYPE
+       res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
+#else
        res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
+#endif
 
        for (i = 0; i < pw1->n; ++i)
                res = FN(PW,add_piece)(res,
@@ -311,10 +363,21 @@ __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *se
        if (!pw)
                goto error;
 
-       for (i = 0; i < pw->n; ++i) {
+       for (i = pw->n - 1; i >= 0; --i) {
+               isl_basic_set *aff;
                pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
                if (!pw->p[i].set)
                        goto error;
+               aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
+               pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
+                                                               aff);
+               if (isl_set_fast_is_empty(pw->p[i].set)) {
+                       isl_set_free(pw->p[i].set);
+                       FN(EL,free)(pw->p[i].FIELD);
+                       if (i != pw->n - 1)
+                               pw->p[i] = pw->p[pw->n - 1];
+                       pw->n--;
+               }
        }
        
        isl_set_free(set);
@@ -338,17 +401,31 @@ __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
                return pw;
        }
 
+       context = isl_set_compute_divs(context);
        hull = isl_set_simple_hull(isl_set_copy(context));
 
        pw = FN(PW,cow)(pw);
        if (!pw)
                goto error;
 
-       for (i = 0; i < pw->n; ++i) {
+       for (i = pw->n - 1; i >= 0; --i) {
+               pw->p[i].set = isl_set_intersect(pw->p[i].set,
+                                                isl_set_copy(context));
+               if (!pw->p[i].set)
+                       goto error;
+               pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
+                                            isl_set_copy(pw->p[i].set));
                pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
                                                isl_basic_set_copy(hull));
                if (!pw->p[i].set)
                        goto error;
+               if (isl_set_fast_is_empty(pw->p[i].set)) {
+                       isl_set_free(pw->p[i].set);
+                       FN(EL,free)(pw->p[i].FIELD);
+                       if (i != pw->n - 1)
+                               pw->p[i] = pw->p[pw->n - 1];
+                       pw->n--;
+               }
        }
 
        isl_basic_set_free(hull);
@@ -419,6 +496,34 @@ int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
        return 0;
 }
 
+__isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
+       enum isl_dim_type type, unsigned pos, const char *s)
+{
+       int i;
+
+       pw = FN(PW,cow)(pw);
+       if (!pw)
+               return NULL;
+
+       pw->dim = isl_dim_set_name(pw->dim, type, pos, s);
+       if (!pw->dim)
+               goto error;
+
+       for (i = 0; i < pw->n; ++i) {
+               pw->p[i].set = isl_set_set_dim_name(pw->p[i].set, type, pos, s);
+               if (!pw->p[i].set)
+                       goto error;
+               pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
+               if (!pw->p[i].FIELD)
+                       goto error;
+       }
+
+       return pw;
+error:
+       FN(PW,free)(pw);
+       return NULL;
+}
+
 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
        enum isl_dim_type type, unsigned first, unsigned n)
 {
@@ -450,6 +555,40 @@ error:
        return NULL;
 }
 
+__isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
+       unsigned first, unsigned n)
+{
+       int i;
+
+       if (!pw)
+               return NULL;
+       if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
+               return pw;
+
+       pw = FN(PW,cow)(pw);
+       if (!pw)
+               return NULL;
+
+       pw->dim = isl_dim_insert(pw->dim, type, first, n);
+       if (!pw->dim)
+               goto error;
+
+       for (i = 0; i < pw->n; ++i) {
+               pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
+               if (!pw->p[i].set)
+                       goto error;
+               pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
+                                                               type, first, n);
+               if (!pw->p[i].FIELD)
+                       goto error;
+       }
+
+       return pw;
+error:
+       FN(PW,free)(pw);
+       return NULL;
+}
+
 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
        enum isl_dim_type type, unsigned pos, isl_int v)
 {
@@ -555,6 +694,42 @@ __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
        return pw ? isl_dim_copy(pw->dim) : NULL;
 }
 
+__isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
+{
+       int i;
+
+       pw = FN(PW,cow)(pw);
+       if (!pw || !dim)
+               goto error;
+
+       for (i = 0; i < pw->n; ++i) {
+               pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
+                                                isl_dim_copy(dim));
+               if (!pw->p[i].set)
+                       goto error;
+               pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
+                                                 isl_dim_copy(dim));
+               if (!pw->p[i].FIELD)
+                       goto error;
+       }
+       isl_dim_free(pw->dim);
+       pw->dim = dim;
+
+       return pw;
+error:
+       isl_dim_free(dim);
+       FN(PW,free)(pw);
+       return NULL;
+}
+
+int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
+{
+       if (!pw1 || !pw2)
+               return -1;
+
+       return isl_dim_equal(pw1->dim, pw2->dim);
+}
+
 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
 {
        int i;
@@ -714,3 +889,32 @@ error:
        FN(PW,free)(pw);
        return NULL;
 }
+
+__isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
+{
+       int i;
+
+       pw = FN(PW,cow)(pw);
+       if (!pw || !exp)
+               return NULL;
+
+       for (i = 0; i < pw->n; ++i) {
+               pw->p[i].set = isl_set_realign(pw->p[i].set,
+                                                   isl_reordering_copy(exp));
+               if (!pw->p[i].set)
+                       goto error;
+               pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
+                                                   isl_reordering_copy(exp));
+               if (!pw->p[i].FIELD)
+                       goto error;
+       }
+
+       pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
+
+       isl_reordering_free(exp);
+       return pw;
+error:
+       isl_reordering_free(exp);
+       FN(PW,free)(pw);
+       return NULL;
+}