isl_stream: accept "@" token
[platform/upstream/isl.git] / isl_dim.c
index 71b0c47..c364c8b 100644 (file)
--- a/isl_dim.c
+++ b/isl_dim.c
@@ -1,4 +1,13 @@
-#include "isl_dim.h"
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
+#include <isl_dim_private.h>
 #include "isl_name.h"
 
 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
@@ -45,11 +54,44 @@ static unsigned global_pos(struct isl_dim *dim,
                isl_assert(ctx, pos < dim->n_out, return isl_dim_total(dim));
                return pos + dim->nparam + dim->n_in;
        default:
-               isl_assert(ctx, 0, goto error);
+               isl_assert(ctx, 0, return isl_dim_total(dim));
        }
        return isl_dim_total(dim);
 }
 
+/* Extend length of names array to the total number of dimensions.
+ */
+static __isl_give isl_dim *extend_names(__isl_take isl_dim *dim)
+{
+       struct isl_name **names;
+       int i;
+
+       if (isl_dim_total(dim) <= dim->n_name)
+               return dim;
+
+       if (!dim->names) {
+               dim->names = isl_calloc_array(dim->ctx,
+                               struct isl_name *, isl_dim_total(dim));
+               if (!dim->names)
+                       goto error;
+       } else {
+               names = isl_realloc_array(dim->ctx, dim->names,
+                               struct isl_name *, isl_dim_total(dim));
+               if (!names)
+                       goto error;
+               dim->names = names;
+               for (i = dim->n_name; i < isl_dim_total(dim); ++i)
+                       dim->names[i] = NULL;
+       }
+
+       dim->n_name = isl_dim_total(dim);
+
+       return dim;
+error:
+       isl_dim_free(dim);
+       return NULL;
+}
+
 static struct isl_dim *set_name(struct isl_dim *dim,
                                 enum isl_dim_type type, unsigned pos,
                                 struct isl_name *name)
@@ -66,21 +108,9 @@ static struct isl_dim *set_name(struct isl_dim *dim,
        if (pos >= dim->n_name) {
                if (!name)
                        return dim;
-               if (!dim->names) {
-                       dim->names = isl_calloc_array(dim->ctx,
-                                       struct isl_name *, isl_dim_total(dim));
-                       if (!dim->names)
-                               goto error;
-               } else {
-                       int i;
-                       dim->names = isl_realloc_array(dim->ctx, dim->names,
-                                       struct isl_name *, isl_dim_total(dim));
-                       if (!dim->names)
-                               goto error;
-                       for (i = dim->n_name; i < isl_dim_total(dim); ++i)
-                               dim->names[i] = NULL;
-               }
-               dim->n_name = isl_dim_total(dim);
+               dim = extend_names(dim);
+               if (!dim)
+                       goto error;
        }
 
        dim->names[pos] = name;
@@ -106,6 +136,15 @@ static struct isl_name *get_name(struct isl_dim *dim,
        return dim->names[pos];
 }
 
+static unsigned offset(struct isl_dim *dim, enum isl_dim_type type)
+{
+       switch (type) {
+       case isl_dim_param:     return 0;
+       case isl_dim_in:        return dim->nparam;
+       case isl_dim_out:       return dim->nparam + dim->n_in;
+       }
+}
+
 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
 {
        switch (type) {
@@ -115,18 +154,26 @@ static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
        }
 }
 
+unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
+{
+       if (!dim)
+               return 0;
+       return n(dim, type);
+}
+
 static struct isl_dim *copy_names(struct isl_dim *dst,
-       enum isl_dim_type dst_type, struct isl_dim *src,
+       enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
        enum isl_dim_type src_type)
 {
        int i;
        struct isl_name *name;
 
-       for (i = 0; i < n(dst, dst_type); ++i) {
+       for (i = 0; i < n(src, src_type); ++i) {
                name = get_name(src, src_type, i);
                if (!name)
                        continue;
-               dst = set_name(dst, dst_type, i, isl_name_copy(dst->ctx, name));
+               dst = set_name(dst, dst_type, offset + i,
+                                       isl_name_copy(dst->ctx, name));
                if (!dst)
                        return NULL;
        }
@@ -139,9 +186,9 @@ struct isl_dim *isl_dim_dup(struct isl_dim *dim)
        dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
        if (!dim->names)
                return dup;
-       dup = copy_names(dup, isl_dim_param, dim, isl_dim_param);
-       dup = copy_names(dup, isl_dim_in, dim, isl_dim_in);
-       dup = copy_names(dup, isl_dim_out, dim, isl_dim_out);
+       dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
+       dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
+       dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
        return dup;
 }
 
@@ -225,6 +272,12 @@ static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
        return 1;
 }
 
+int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
+               struct isl_dim *dim2, enum isl_dim_type dim2_type)
+{
+       return match(dim1, dim1_type, dim2, dim2_type);
+}
+
 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
        unsigned first, unsigned n, struct isl_name **names)
 {
@@ -274,6 +327,66 @@ error:
        return NULL;
 }
 
+struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
+       unsigned n)
+{
+       switch (type) {
+       case isl_dim_param:
+               return isl_dim_extend(dim,
+                                       dim->nparam + n, dim->n_in, dim->n_out);
+       case isl_dim_in:
+               return isl_dim_extend(dim,
+                                       dim->nparam, dim->n_in + n, dim->n_out);
+       case isl_dim_out:
+               return isl_dim_extend(dim,
+                                       dim->nparam, dim->n_in, dim->n_out + n);
+       }
+       return dim;
+}
+
+__isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
+       enum isl_dim_type dst_type, unsigned dst_pos,
+       enum isl_dim_type src_type, unsigned src_pos, unsigned n)
+{
+       if (!dim)
+               return NULL;
+       if (n == 0)
+               return dim;
+
+       isl_assert(dim->ctx, src_pos + n <= isl_dim_size(dim, src_type),
+               goto error);
+
+       /* just the simple case for now */
+       isl_assert(dim->ctx,
+               offset(dim, dst_type) + dst_pos ==
+               offset(dim, src_type) + src_pos + ((src_type < dst_type) ? n : 0),
+               goto error);
+
+       if (dst_type == src_type)
+               return dim;
+
+       dim = isl_dim_cow(dim);
+       if (!dim)
+               return NULL;
+
+       switch (dst_type) {
+       case isl_dim_param:     dim->nparam += n; break;
+       case isl_dim_in:        dim->n_in += n; break;
+       case isl_dim_out:       dim->n_out += n; break;
+       }
+
+       switch (src_type) {
+       case isl_dim_param:     dim->nparam -= n; break;
+       case isl_dim_in:        dim->n_in -= n; break;
+       case isl_dim_out:       dim->n_out -= n; break;
+       }
+
+       return dim;
+error:
+       isl_dim_free(dim);
+       return NULL;
+}
+
 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
 {
        struct isl_dim *dim;
@@ -283,16 +396,47 @@ struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
 
        isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
                        goto error);
-       isl_assert(left->ctx, match(left, isl_dim_out, right, isl_dim_in),
+       isl_assert(left->ctx, n(left, isl_dim_out) == n(right, isl_dim_in),
                        goto error);
 
        dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
        if (!dim)
                goto error;
 
-       dim = copy_names(dim, isl_dim_param, left, isl_dim_param);
-       dim = copy_names(dim, isl_dim_in, left, isl_dim_in);
-       dim = copy_names(dim, isl_dim_out, right, isl_dim_out);
+       dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
+       dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
+       dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
+
+       isl_dim_free(left);
+       isl_dim_free(right);
+
+       return dim;
+error:
+       isl_dim_free(left);
+       isl_dim_free(right);
+       return NULL;
+}
+
+struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
+{
+       struct isl_dim *dim;
+
+       if (!left || !right)
+               goto error;
+
+       isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
+                       goto error);
+
+       dim = isl_dim_alloc(left->ctx, left->nparam,
+                       left->n_in + right->n_in, left->n_out + right->n_out);
+       if (!dim)
+               goto error;
+
+       dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
+       dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
+       dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
+       dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
+       dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
 
        isl_dim_free(left);
        isl_dim_free(right);
@@ -326,10 +470,10 @@ struct isl_dim *isl_dim_map(struct isl_dim *dim)
        }
        dim->n_in = dim->n_out;
        if (names) {
-               copy_names(dim, isl_dim_out, dim, isl_dim_in);
                free(dim->names);
                dim->names = names;
                dim->n_name = dim->nparam + dim->n_out + dim->n_out;
+               dim = copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
        }
        return dim;
 error:
@@ -388,8 +532,8 @@ error:
        return NULL;
 }
 
-struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
-               unsigned first, unsigned n)
+struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
+               unsigned first, unsigned num)
 {
        int i;
 
@@ -399,57 +543,51 @@ struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
        if (n == 0)
                return dim;
 
-       isl_assert(dim->ctx, first + n <= dim->n_in, goto error);
+       isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
        dim = isl_dim_cow(dim);
        if (!dim)
                goto error;
        if (dim->names) {
-               for (i = 0; i < n; ++i) {
-                       isl_name_free(dim->ctx,
-                                       get_name(dim, isl_dim_in, first+i));
+               dim = extend_names(dim);
+               if (!dim)
+                       goto error;
+               for (i = 0; i < num; ++i)
+                       isl_name_free(dim->ctx, get_name(dim, type, first+i));
+               for (i = first+num; i < n(dim, type); ++i)
+                       set_name(dim, type, i - num, get_name(dim, type, i));
+               switch (type) {
+               case isl_dim_param:
+                       get_names(dim, isl_dim_in, 0, dim->n_in,
+                               dim->names + offset(dim, isl_dim_in) - num);
+               case isl_dim_in:
+                       get_names(dim, isl_dim_out, 0, dim->n_out,
+                               dim->names + offset(dim, isl_dim_out) - num);
+               case isl_dim_out:
+                       ;
                }
-               for (i = first+n; i < dim->n_in; ++i)
-                       set_name(dim, isl_dim_in, i - n,
-                               get_name(dim, isl_dim_in, i));
-               get_names(dim, isl_dim_out, 0, dim->n_out,
-                               dim->names + dim->nparam + dim->n_in - n);
+               dim->n_name -= num;
+       }
+       switch (type) {
+       case isl_dim_param:     dim->nparam -= num; break;
+       case isl_dim_in:        dim->n_in -= num; break;
+       case isl_dim_out:       dim->n_out -= num; break;
        }
-       dim->n_in -= n;
        return dim;
 error:
        isl_dim_free(dim);
        return NULL;
 }
 
-struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
+struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
                unsigned first, unsigned n)
 {
-       int i;
-
-       if (!dim)
-               return NULL;
-
-       if (n == 0)
-               return dim;
+       return isl_dim_drop(dim, isl_dim_in, first, n);
+}
 
-       isl_assert(dim->ctx, first + n <= dim->n_out, goto error);
-       dim = isl_dim_cow(dim);
-       if (!dim)
-               goto error;
-       if (dim->names) {
-               for (i = 0; i < n; ++i) {
-                       isl_name_free(dim->ctx,
-                                       get_name(dim, isl_dim_out, first+i));
-               }
-               for (i = first+n; i < dim->n_out; ++i)
-                       set_name(dim, isl_dim_out, i - n,
-                               get_name(dim, isl_dim_out, i));
-       }
-       dim->n_out -= n;
-       return dim;
-error:
-       isl_dim_free(dim);
-       return NULL;
+struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
+               unsigned first, unsigned n)
+{
+       return isl_dim_drop(dim, isl_dim_out, first, n);
 }
 
 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
@@ -460,6 +598,13 @@ struct isl_dim *isl_dim_domain(struct isl_dim *dim)
        return isl_dim_reverse(dim);
 }
 
+struct isl_dim *isl_dim_range(struct isl_dim *dim)
+{
+       if (!dim)
+               return NULL;
+       return isl_dim_drop_inputs(dim, 0, dim->n_in);
+}
+
 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
 {
        int i;
@@ -491,8 +636,8 @@ unsigned isl_dim_total(struct isl_dim *dim)
 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
 {
        return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
-              match(dim1, isl_dim_in, dim2, isl_dim_in) &&
-              match(dim1, isl_dim_out, dim2, isl_dim_out);
+              n(dim1, isl_dim_in) == n(dim2, isl_dim_in) &&
+              n(dim1, isl_dim_out) == n(dim2, isl_dim_out);
 }
 
 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)