add isl_printer_print_double
[platform/upstream/isl.git] / isl_pw_templ.c
index 212d8a0..fba2fa8 100644 (file)
@@ -173,11 +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;
+}
+
+int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
+{
+       return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
+}
+
+__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;
@@ -199,7 +221,7 @@ __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
 
        pw = FN(PW,cow)(pw);
        if (!pw || !exp)
-               return NULL;
+               goto error;
 
        for (i = 0; i < pw->n; ++i) {
                pw->p[i].set = isl_set_realign(pw->p[i].set,
@@ -308,7 +330,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;
@@ -348,14 +371,14 @@ static __isl_give PW *FN(PW,add_aligned)(__isl_take PW *pw1, __isl_take PW *pw2)
                for (j = 0; j < pw2->n; ++j) {
                        struct isl_set *common;
                        EL *sum;
-                       set = isl_set_subtract(set,
-                                       isl_set_copy(pw2->p[j].set));
                        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;
                        }
+                       set = isl_set_subtract(set,
+                                       isl_set_copy(pw2->p[j].set));
 
                        sum = FN(EL,add_on_domain)(common,
                                                   FN(EL,copy)(pw1->p[i].FIELD),
@@ -384,9 +407,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 FN(PW,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.
@@ -482,6 +509,96 @@ __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *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_in)(__isl_take PW *pw1,
+       __isl_take PW *pw2, __isl_take isl_space *space,
+       __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.
+ * The result of "fn" (and therefore also of this function) lives in "space".
+ */
+static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
+       __isl_take PW *pw2, __isl_take isl_space *space,
+       __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
+{
+       int i, j, n;
+       PW *res = NULL;
+
+       if (!pw1 || !pw2)
+               goto error;
+
+       n = pw1->n * pw2->n;
+#ifdef HAS_TYPE
+       res = FN(PW,alloc_size)(space, pw1->type, n);
+#else
+       res = FN(PW,alloc_size)(space, n);
+#endif
+
+       for (i = 0; i < pw1->n; ++i) {
+               for (j = 0; j < pw2->n; ++j) {
+                       isl_set *common;
+                       EL *res_ij;
+                       int empty;
+
+                       common = isl_set_intersect(
+                                       isl_set_copy(pw1->p[i].set),
+                                       isl_set_copy(pw2->p[j].set));
+                       empty = isl_set_plain_is_empty(common);
+                       if (empty < 0 || empty) {
+                               isl_set_free(common);
+                               if (empty < 0)
+                                       goto error;
+                               continue;
+                       }
+
+                       res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
+                                   FN(EL,copy)(pw2->p[j].FIELD));
+                       res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
+
+                       res = FN(PW,add_piece)(res, common, res_ij);
+               }
+       }
+
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return res;
+error:
+       isl_space_free(space);
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       FN(PW,free)(res);
+       return NULL;
+}
+
+/* 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.
+ * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
+ */
+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))
+{
+       isl_space *space;
+
+       if (!pw1 || !pw2)
+               goto error;
+
+       space = isl_space_copy(pw1->dim);
+       return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
+error:
+       FN(PW,free)(pw1);
+       FN(PW,free)(pw2);
+       return NULL;
+}
+
 #ifndef NO_NEG
 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
 {
@@ -569,8 +686,14 @@ __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
        return dom;
 }
 
-static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
-       __isl_take isl_set *set)
+/* Restrict the domain of "pw" by combining each cell
+ * with "set" through a call to "fn", where "fn" may be
+ * isl_set_intersect or isl_set_intersect_params.
+ */
+static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *set,
+       __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
+                                   __isl_take isl_set *set2))
 {
        int i;
 
@@ -588,12 +711,14 @@ static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
 
        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));
+               pw->p[i].set = fn(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 (!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);
@@ -611,6 +736,12 @@ error:
        return NULL;
 }
 
+static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *set)
+{
+       return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
+}
+
 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
        __isl_take isl_set *context)
 {
@@ -618,9 +749,28 @@ __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
                                        &FN(PW,intersect_domain_aligned));
 }
 
-static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
+static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *set)
+{
+       return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
+}
+
+/* Intersect the domain of "pw" with the parameter domain "context".
+ */
+__isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
        __isl_take isl_set *context)
 {
+       return FN(PW,align_params_pw_set_and)(pw, context,
+                                       &FN(PW,intersect_params_aligned));
+}
+
+static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *context,
+       __isl_give EL *(*fn_el)(__isl_take EL *el,
+                                   __isl_take isl_set *set),
+       __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
+                                   __isl_take isl_basic_set *bset))
+{
        int i;
        isl_basic_set *hull = NULL;
 
@@ -650,10 +800,9 @@ static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
                                                 isl_set_copy(context));
                if (!pw->p[i].set)
                        goto error;
-               pw->p[i].FIELD = FN(EL,gist)(pw->p[i].FIELD,
+               pw->p[i].FIELD = fn_el(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));
+               pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
                if (!pw->p[i].set)
                        goto error;
                if (isl_set_plain_is_empty(pw->p[i].set)) {
@@ -676,9 +825,31 @@ error:
        return NULL;
 }
 
+static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *set)
+{
+       return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
+                                       &isl_set_gist_basic_set);
+}
+
 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
 {
-       return FN(PW,align_params_pw_set_and)(pw, context, &FN(PW,gist_aligned));
+       return FN(PW,align_params_pw_set_and)(pw, context,
+                                               &FN(PW,gist_domain_aligned));
+}
+
+static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
+       __isl_take isl_set *set)
+{
+       return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
+                                       &isl_set_gist_params_basic_set);
+}
+
+__isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
+       __isl_take isl_set *context)
+{
+       return FN(PW,align_params_pw_set_and)(pw, context,
+                                               &FN(PW,gist_params_aligned));
 }
 
 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
@@ -990,7 +1161,7 @@ __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
        if (pw->n == 0) {
                isl_space *dim = isl_space_copy(pw->dim);
                FN(PW,free)(pw);
-               return isl_qpolynomial_zero_on_domain(dim);
+               return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
        }
 
        opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
@@ -1100,6 +1271,16 @@ __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
 
        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)
@@ -1152,6 +1333,11 @@ error:
 }
 #endif
 
+int FN(PW,n_piece)(__isl_keep PW *pw)
+{
+       return pw ? pw->n : 0;
+}
+
 int FN(PW,foreach_piece)(__isl_keep PW *pw,
        int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
        void *user)
@@ -1293,7 +1479,7 @@ __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
 
        if (isl_int_is_one(v))
                return pw;
-       if (pw && isl_int_is_zero(v)) {
+       if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
                PW *zero;
                isl_space *dim = FN(PW,get_space)(pw);
 #ifdef HAS_TYPE