add isl_aff_mod_val
[platform/upstream/isl.git] / isl_union_templ.c
index 1ee7d05..cd55f4e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2010      INRIA Saclay
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
@@ -70,12 +70,12 @@ error:
 }
 
 #ifdef HAS_TYPE
-__isl_give UNION *FN(UNION,zero)(__isl_take isl_space *dim, enum isl_fold type)
+__isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
 {
        return FN(UNION,alloc)(dim, type, 16);
 }
 #else
-__isl_give UNION *FN(UNION,zero)(__isl_take isl_space *dim)
+__isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim)
 {
        return FN(UNION,alloc)(dim, 16);
 }
@@ -138,9 +138,9 @@ __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
                                    &has_dim, dim, 0);
        if (!entry)
 #ifdef HAS_TYPE
-               return FN(PART,zero)(dim, u->type);
+               return FN(PART,ZERO)(dim, u->type);
 #else
-               return FN(PART,zero)(dim);
+               return FN(PART,ZERO)(dim);
 #endif
        isl_space_free(dim);
        return FN(PART,copy)(entry->data);
@@ -158,7 +158,7 @@ __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
        if (!part)
                goto error;
 
-       if (FN(PART,is_zero)(part)) {
+       if (DEFAULT_IS_ZERO && FN(PART,IS_ZERO)(part)) {
                FN(PART,free)(part);
                return u;
        }
@@ -184,7 +184,7 @@ __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
                if (!entry->data)
                        goto error;
                FN(PART,free)(part);
-               if (FN(PART,is_zero)(entry->data)) {
+               if (DEFAULT_IS_ZERO && FN(PART,IS_ZERO)(entry->data)) {
                        FN(PART,free)(entry->data);
                        isl_hash_table_remove(u->dim->ctx, &u->table, entry);
                }
@@ -214,9 +214,9 @@ __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
                return NULL;
 
 #ifdef HAS_TYPE
-       dup = FN(UNION,zero)(isl_space_copy(u->dim), u->type);
+       dup = FN(UNION,ZERO)(isl_space_copy(u->dim), u->type);
 #else
-       dup = FN(UNION,zero)(isl_space_copy(u->dim));
+       dup = FN(UNION,ZERO)(isl_space_copy(u->dim));
 #endif
        if (FN(FN(UNION,foreach),PARTS)(u, &add_part, &dup) < 0)
                goto error;
@@ -244,18 +244,19 @@ static int free_u_entry(void **entry, void *user)
        return 0;
 }
 
-void FN(UNION,free)(__isl_take UNION *u)
+void *FN(UNION,free)(__isl_take UNION *u)
 {
        if (!u)
-               return;
+               return NULL;
 
        if (--u->ref > 0)
-               return;
+               return NULL;
 
        isl_hash_table_foreach(u->dim->ctx, &u->table, &free_u_entry, NULL);
        isl_hash_table_clear(&u->table);
        isl_space_free(u->dim);
        free(u);
+       return NULL;
 }
 
 S(UNION,align) {
@@ -365,9 +366,9 @@ __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
        dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
        dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
 #ifdef HAS_TYPE
-       u = FN(UNION,zero)(dim, part->type);
+       u = FN(UNION,ZERO)(dim, part->type);
 #else
-       u = FN(UNION,zero)(dim);
+       u = FN(UNION,ZERO)(dim);
 #endif
        u = FN(FN(UNION,add),PARTS)(u, part);
 
@@ -377,18 +378,66 @@ __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
 S(UNION,match_bin_data) {
        UNION *u2;
        UNION *res;
+       __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
 };
 
+/* Check if data->u2 has an element living in the same space as *entry.
+ * If so, call data->fn on the two elements and add the result to
+ * data->res.
+ */
+static int match_bin_entry(void **entry, void *user)
+{
+       S(UNION,match_bin_data) *data = user;
+       uint32_t hash;
+       struct isl_hash_table_entry *entry2;
+       isl_space *space;
+       PART *part = *entry;
+
+       space = FN(PART,get_space)(part);
+       hash = isl_space_get_hash(space);
+       entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
+                                    hash, &has_dim, space, 0);
+       isl_space_free(space);
+       if (!entry2)
+               return 0;
+
+       part = FN(PART, copy)(part);
+       part = data->fn(part, FN(PART, copy)(entry2->data));
+
+       if (DEFAULT_IS_ZERO) {
+               int empty;
+
+               empty = FN(PART,IS_ZERO)(part);
+               if (empty < 0) {
+                       FN(PART,free)(part);
+                       return -1;
+               }
+               if (empty) {
+                       FN(PART,free)(part);
+                       return 0;
+               }
+       }
+
+       data->res = FN(FN(UNION,add),PARTS)(data->res, part);
+
+       return 0;
+}
+
 /* This function is currently only used from isl_polynomial.c
  * and not from isl_fold.c.
  */
 static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
        __isl_take UNION *u2,
-       int (*fn)(void **, void *)) __attribute__ ((unused));
+       __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
+       __attribute__ ((unused));
+/* For each pair of elements in "u1" and "u2" living in the same space,
+ * call "fn" and collect the results.
+ */
 static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
-       __isl_take UNION *u2, int (*fn)(void **, void *))
+       __isl_take UNION *u2,
+       __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
 {
-       S(UNION,match_bin_data) data = { NULL, NULL };
+       S(UNION,match_bin_data) data = { NULL, NULL, fn };
 
        u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
        u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
@@ -402,7 +451,8 @@ static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
 #else
        data.res = FN(UNION,alloc)(isl_space_copy(u1->dim), u1->table.n);
 #endif
-       if (isl_hash_table_foreach(u1->dim->ctx, &u1->table, fn, &data) < 0)
+       if (isl_hash_table_foreach(u1->dim->ctx, &u1->table,
+                                   &match_bin_entry, &data) < 0)
                goto error;
 
        FN(UNION,free)(u1);
@@ -415,7 +465,100 @@ error:
        return NULL;
 }
 
-S(UNION,match_set_data) {
+#ifndef NO_SUB
+/* Subtract "u2" from "u1" and return the result.
+ */
+__isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
+{
+       return match_bin_op(u1, u2, &FN(PART,sub));
+}
+#endif
+
+S(UNION,any_set_data) {
+       isl_set *set;
+       UNION *res;
+       __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
+};
+
+static int any_set_entry(void **entry, void *user)
+{
+       S(UNION,any_set_data) *data = user;
+       PW *pw = *entry;
+
+       pw = FN(PW,copy)(pw);
+       pw = data->fn(pw, isl_set_copy(data->set));
+
+       if (DEFAULT_IS_ZERO) {
+               int empty;
+
+               empty = FN(PW,IS_ZERO)(pw);
+               if (empty < 0) {
+                       FN(PW,free)(pw);
+                       return -1;
+               }
+               if (empty) {
+                       FN(PW,free)(pw);
+                       return 0;
+               }
+       }
+
+       data->res = FN(FN(UNION,add),PARTS)(data->res, pw);
+
+       return 0;
+}
+
+/* Update each element of "u" by calling "fn" on the element and "set".
+ */
+static __isl_give UNION *any_set_op(__isl_take UNION *u,
+       __isl_take isl_set *set,
+       __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
+{
+       S(UNION,any_set_data) data = { NULL, NULL, fn };
+
+       u = FN(UNION,align_params)(u, isl_set_get_space(set));
+       set = isl_set_align_params(set, FN(UNION,get_space)(u));
+
+       if (!u || !set)
+               goto error;
+
+       data.set = set;
+#ifdef HAS_TYPE
+       data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->type, u->table.n);
+#else
+       data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->table.n);
+#endif
+       if (isl_hash_table_foreach(u->dim->ctx, &u->table,
+                                  &any_set_entry, &data) < 0)
+               goto error;
+
+       FN(UNION,free)(u);
+       isl_set_free(set);
+       return data.res;
+error:
+       FN(UNION,free)(u);
+       isl_set_free(set);
+       FN(UNION,free)(data.res);
+       return NULL;
+}
+
+/* Intersect the domain of "u" with the parameter domain "context".
+ */
+__isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
+       __isl_take isl_set *set)
+{
+       return any_set_op(u, set, &FN(PW,intersect_params));
+}
+
+/* Compute the gist of the domain of "u" with respect to
+ * the parameter domain "context".
+ */
+__isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
+       __isl_take isl_set *set)
+{
+       return any_set_op(u, set, &FN(PW,gist_params));
+}
+
+S(UNION,match_domain_data) {
        isl_union_set *uset;
        UNION *res;
        __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
@@ -429,31 +572,41 @@ static int set_has_dim(const void *entry, const void *val)
        return isl_space_is_equal(set->dim, dim);
 }
 
-static int match_set_entry(void **entry, void *user)
+/* Find the set in data->uset that live in the same space as the domain
+ * of *entry, apply data->fn to *entry and this set (if any), and add
+ * the result to data->res.
+ */
+static int match_domain_entry(void **entry, void *user)
 {
-       S(UNION,match_set_data) *data = user;
+       S(UNION,match_domain_data) *data = user;
        uint32_t hash;
        struct isl_hash_table_entry *entry2;
        PW *pw = *entry;
-       int empty;
+       isl_space *space;
 
-       hash = isl_space_get_hash(pw->dim);
+       space = FN(PW,get_domain_space)(pw);
+       hash = isl_space_get_hash(space);
        entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
-                                    hash, &set_has_dim, pw->dim, 0);
+                                    hash, &set_has_dim, space, 0);
+       isl_space_free(space);
        if (!entry2)
                return 0;
 
        pw = FN(PW,copy)(pw);
        pw = data->fn(pw, isl_set_copy(entry2->data));
 
-       empty = FN(PW,is_zero)(pw);
-       if (empty < 0) {
-               FN(PW,free)(pw);
-               return -1;
-       }
-       if (empty) {
-               FN(PW,free)(pw);
-               return 0;
+       if (DEFAULT_IS_ZERO) {
+               int empty;
+
+               empty = FN(PW,IS_ZERO)(pw);
+               if (empty < 0) {
+                       FN(PW,free)(pw);
+                       return -1;
+               }
+               if (empty) {
+                       FN(PW,free)(pw);
+                       return 0;
+               }
        }
 
        data->res = FN(FN(UNION,add),PARTS)(data->res, pw);
@@ -461,11 +614,15 @@ static int match_set_entry(void **entry, void *user)
        return 0;
 }
 
-static __isl_give UNION *match_set_op(__isl_take UNION *u,
+/* Apply fn to each pair of PW in u and set in uset such that
+ * the set lives in the same space as the domain of PW
+ * and collect the results.
+ */
+static __isl_give UNION *match_domain_op(__isl_take UNION *u,
        __isl_take isl_union_set *uset,
        __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
 {
-       S(UNION,match_set_data) data = { NULL, NULL, fn };
+       S(UNION,match_domain_data) data = { NULL, NULL, fn };
 
        u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
        uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
@@ -480,7 +637,7 @@ static __isl_give UNION *match_set_op(__isl_take UNION *u,
        data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->table.n);
 #endif
        if (isl_hash_table_foreach(u->dim->ctx, &u->table,
-                                  &match_set_entry, &data) < 0)
+                                  &match_domain_entry, &data) < 0)
                goto error;
 
        FN(UNION,free)(u);
@@ -493,18 +650,28 @@ error:
        return NULL;
 }
 
+/* Intersect the domain of "u" with "uset".
+ * If "uset" is a parameters domain, then intersect the parameter
+ * domain of "u" with this set.
+ */
 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
        __isl_take isl_union_set *uset)
 {
-       return match_set_op(u, uset, &FN(PW,intersect_domain));
+       if (isl_union_set_is_params(uset))
+               return FN(UNION,intersect_params)(u,
+                                               isl_set_from_union_set(uset));
+       return match_domain_op(u, uset, &FN(PW,intersect_domain));
 }
 
 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
        __isl_take isl_union_set *uset)
 {
-       return match_set_op(u, uset, &FN(PW,gist));
+       if (isl_union_set_is_params(uset))
+               return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
+       return match_domain_op(u, uset, &FN(PW,gist));
 }
 
+#ifndef NO_EVAL
 __isl_give isl_qpolynomial *FN(UNION,eval)(__isl_take UNION *u,
        __isl_take isl_point *pnt)
 {
@@ -538,6 +705,7 @@ error:
        isl_point_free(pnt);
        return NULL;
 }
+#endif
 
 static int coalesce_entry(void **entry, void *user)
 {
@@ -608,13 +776,13 @@ __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
        if (isl_int_is_one(v))
                return u;
 
-       if (u && isl_int_is_zero(v)) {
+       if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
                UNION *zero;
                isl_space *dim = FN(UNION,get_space)(u);
 #ifdef HAS_TYPE
-               zero = FN(UNION,zero)(dim, u->type);
+               zero = FN(UNION,ZERO)(dim, u->type);
 #else
-               zero = FN(UNION,zero)(dim);
+               zero = FN(UNION,ZERO)(dim);
 #endif
                FN(UNION,free)(u);
                return zero;
@@ -636,3 +804,65 @@ error:
        FN(UNION,free)(u);
        return NULL;
 }
+
+S(UNION,plain_is_equal_data)
+{
+       UNION *u2;
+       int is_equal;
+};
+
+static int plain_is_equal_entry(void **entry, void *user)
+{
+       S(UNION,plain_is_equal_data) *data = user;
+       uint32_t hash;
+       struct isl_hash_table_entry *entry2;
+       PW *pw = *entry;
+
+       hash = isl_space_get_hash(pw->dim);
+       entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
+                                    hash, &has_dim, pw->dim, 0);
+       if (!entry2) {
+               data->is_equal = 0;
+               return -1;
+       }
+
+       data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
+       if (data->is_equal < 0 || !data->is_equal)
+               return -1;
+
+       return 0;
+}
+
+int FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
+{
+       S(UNION,plain_is_equal_data) data = { NULL, 1 };
+
+       if (!u1 || !u2)
+               return -1;
+       if (u1 == u2)
+               return 1;
+       if (u1->table.n != u2->table.n)
+               return 0;
+
+       u1 = FN(UNION,copy)(u1);
+       u2 = FN(UNION,copy)(u2);
+       u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
+       u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
+       if (!u1 || !u2)
+               goto error;
+
+       data.u2 = u2;
+       if (isl_hash_table_foreach(u1->dim->ctx, &u1->table,
+                                  &plain_is_equal_entry, &data) < 0 &&
+           data.is_equal)
+               goto error;
+
+       FN(UNION,free)(u1);
+       FN(UNION,free)(u2);
+
+       return data.is_equal;
+error:
+       FN(UNION,free)(u1);
+       FN(UNION,free)(u2);
+       return -1;
+}