rename isl_pw_aff_add to isl_pw_aff_union_add
[platform/upstream/isl.git] / isl_pw_templ.c
index ae9328d..ddabbc5 100644 (file)
@@ -173,6 +173,33 @@ void *FN(PW,free)(__isl_take PW *pw)
        return NULL;
 }
 
+const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
+       unsigned pos)
+{
+       return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
+}
+
+__isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
+       unsigned pos)
+{
+       return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
+}
+
+const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
+{
+       return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
+}
+
+int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
+{
+       return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
+}
+
+__isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
+{
+       return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
+}
+
 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
 {
        if (!pw)
@@ -249,7 +276,7 @@ error:
        return NULL;
 }
 
-static __isl_give PW *align_params_pw_pw_and(__isl_take PW *pw1,
+static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
        __isl_take PW *pw2,
        __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
 {
@@ -273,7 +300,7 @@ error:
        return NULL;
 }
 
-static __isl_give PW *align_params_pw_set_and(__isl_take PW *pw,
+static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
        __isl_take isl_set *set,
        __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
 {
@@ -298,7 +325,8 @@ error:
 }
 #endif
 
-static __isl_give PW *FN(PW,add_aligned)(__isl_take PW *pw1, __isl_take PW *pw2)
+static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
+       __isl_take PW *pw2)
 {
        int i, j, n;
        struct PW *res;
@@ -374,9 +402,13 @@ error:
        return NULL;
 }
 
-__isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
+/* Private version of "union_add".  For isl_pw_qpolynomial and
+ * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
+ */
+static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
 {
-       return align_params_pw_pw_and(pw1, pw2, &FN(PW,add_aligned));
+       return FN(PW,align_params_pw_pw_and)(pw1, pw2,
+                                               &FN(PW,union_add_aligned));
 }
 
 /* Make sure "pw" has room for at least "n" more pieces.
@@ -468,7 +500,62 @@ error:
 
 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
 {
-       return align_params_pw_pw_and(pw1, pw2, &FN(PW,add_disjoint_aligned));
+       return FN(PW,align_params_pw_pw_and)(pw1, pw2,
+                                               &FN(PW,add_disjoint_aligned));
+}
+
+/* This function is currently only used from isl_aff.c
+ */
+static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
+       __isl_take PW *pw2,
+       __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
+       __attribute__ ((unused));
+
+/* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
+ */
+static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
+       __isl_take PW *pw2,
+       __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
+{
+       int i, j, n;
+       PW *res;
+
+       if (!pw1 || !pw2)
+               goto error;
+
+       n = pw1->n * pw2->n;
+#ifdef HAS_TYPE
+       res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
+#else
+       res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
+#endif
+
+       for (i = 0; i < pw1->n; ++i) {
+               for (j = 0; j < pw2->n; ++j) {
+                       isl_set *common;
+                       EL *res_ij;
+                       common = isl_set_intersect(
+                                       isl_set_copy(pw1->p[i].set),
+                                       isl_set_copy(pw2->p[j].set));
+                       if (isl_set_plain_is_empty(common)) {
+                               isl_set_free(common);
+                               continue;
+                       }
+
+                       res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
+                                   FN(EL,copy)(pw2->p[j].FIELD));
+
+                       res = FN(PW,add_piece)(res, common, res_ij);
+               }
+       }
+
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return res;
+error:
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return NULL;
 }
 
 #ifndef NO_NEG
@@ -583,6 +670,8 @@ static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
                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 (!pw->p[i].FIELD)
+                       goto error;
                if (isl_set_plain_is_empty(pw->p[i].set)) {
                        isl_set_free(pw->p[i].set);
                        FN(EL,free)(pw->p[i].FIELD);
@@ -603,7 +692,7 @@ error:
 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
        __isl_take isl_set *context)
 {
-       return align_params_pw_set_and(pw, context,
+       return FN(PW,align_params_pw_set_and)(pw, context,
                                        &FN(PW,intersect_domain_aligned));
 }
 
@@ -667,7 +756,7 @@ error:
 
 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
 {
-       return align_params_pw_set_and(pw, context, &FN(PW,gist_aligned));
+       return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
 }
 
 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
@@ -1074,6 +1163,31 @@ __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
        domain = isl_space_domain(isl_space_copy(dim));
        return FN(PW,reset_space_and_domain)(pw, dim, domain);
 }
+
+__isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
+       __isl_take isl_id *id)
+{
+       isl_space *space;
+
+       pw = FN(PW,cow)(pw);
+       if (!pw)
+               return isl_id_free(id);
+
+       space = FN(PW,get_space)(pw);
+       space = isl_space_set_tuple_id(space, type, id);
+
+       return FN(PW,reset_space)(pw, space);
+}
+
+__isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
+       enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
+{
+       pw = FN(PW,cow)(pw);
+       if (!pw)
+               return isl_id_free(id);
+       pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
+       return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
+}
 #endif
 
 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
@@ -1304,3 +1418,96 @@ __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
 {
        return FN(PW,mul_isl_int)(pw, v);
 }
+
+static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
+{
+       const isl_set *set1 = *(const isl_set **)p1;
+       const isl_set *set2 = *(const isl_set **)p2;
+
+       return isl_set_plain_cmp(set1, set2);
+}
+
+/* We normalize in place, but if anything goes wrong we need
+ * to return NULL, so we need to make sure we don't change the
+ * meaning of any possible other copies of map.
+ */
+__isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
+{
+       int i, j;
+       isl_set *set;
+
+       if (!pw)
+               return NULL;
+       for (i = 0; i < pw->n; ++i) {
+               set = isl_set_normalize(isl_set_copy(pw->p[i].set));
+               if (!set)
+                       return FN(PW,free)(pw);
+               isl_set_free(pw->p[i].set);
+               pw->p[i].set = set;
+       }
+       qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
+       for (i = pw->n - 1; i >= 1; --i) {
+               if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
+                       continue;
+               if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
+                       continue;
+               set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
+                                   isl_set_copy(pw->p[i].set));
+               if (!set)
+                       return FN(PW,free)(pw);
+               isl_set_free(pw->p[i].set);
+               FN(EL,free)(pw->p[i].FIELD);
+               isl_set_free(pw->p[i - 1].set);
+               pw->p[i - 1].set = set;
+               for (j = i + 1; j < pw->n; ++j)
+                       pw->p[j - 1] = pw->p[j];
+               pw->n--;
+       }
+
+       return pw;
+}
+
+/* Is pw1 obviously equal to pw2?
+ * That is, do they have obviously identical cells and obviously identical
+ * elements on each cell?
+ */
+int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
+{
+       int i;
+       int equal;
+
+       if (!pw1 || !pw2)
+               return -1;
+
+       if (pw1 == pw2)
+               return 1;
+       if (!isl_space_is_equal(pw1->dim, pw2->dim))
+               return 0;
+
+       pw1 = FN(PW,copy)(pw1);
+       pw2 = FN(PW,copy)(pw2);
+       pw1 = FN(PW,normalize)(pw1);
+       pw2 = FN(PW,normalize)(pw2);
+       if (!pw1 || !pw2)
+               goto error;
+
+       equal = pw1->n == pw2->n;
+       for (i = 0; equal && i < pw1->n; ++i) {
+               equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
+               if (equal < 0)
+                       goto error;
+               if (!equal)
+                       break;
+               equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
+               if (equal < 0)
+                       goto error;
+       }
+
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return equal;
+error:
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return -1;
+}