isl_ast_build_get_stride: use isl_val
[platform/upstream/isl.git] / isl_union_templ.c
index dce36c0..65ab08f 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,
@@ -378,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));
@@ -403,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);
@@ -416,6 +465,15 @@ error:
        return NULL;
 }
 
+#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;
@@ -747,6 +805,70 @@ error:
        return NULL;
 }
 
+/* Multiply *entry by the isl_val "user".
+ *
+ * Return 0 on success and -1 on error.
+ */
+static int scale_val(void **entry, void *user)
+{
+       PW **pw = (PW **)entry;
+       isl_val *v = user;
+
+       *pw = FN(PW,scale_val)(*pw, isl_val_copy(v));
+       if (!*pw)
+               return -1;
+
+       return 0;
+}
+
+/* Multiply "u" by "v" and return the result.
+ */
+__isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
+       __isl_take isl_val *v)
+{
+       if (!u || !v)
+               goto error;
+       if (isl_val_is_one(v)) {
+               isl_val_free(v);
+               return u;
+       }
+
+       if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
+               UNION *zero;
+               isl_space *space = FN(UNION,get_space)(u);
+#ifdef HAS_TYPE
+               zero = FN(UNION,ZERO)(space, u->type);
+#else
+               zero = FN(UNION,ZERO)(space);
+#endif
+               FN(UNION,free)(u);
+               isl_val_free(v);
+               return zero;
+       }
+
+       if (!isl_val_is_rat(v))
+               isl_die(isl_val_get_ctx(v), isl_error_invalid,
+                       "expecting rational factor", goto error);
+
+       u = FN(UNION,cow)(u);
+       if (!u)
+               return NULL;
+
+#ifdef HAS_TYPE
+       if (isl_val_is_neg(v))
+               u->type = isl_fold_type_negate(u->type);
+#endif
+       if (isl_hash_table_foreach(u->dim->ctx, &u->table, &scale_val, v) < 0)
+               goto error;
+
+       isl_val_free(v);
+       return u;
+error:
+       isl_val_free(v);
+       FN(UNION,free)(u);
+       return NULL;
+}
+
 S(UNION,plain_is_equal_data)
 {
        UNION *u2;