add isl_vec_insert_zero_els
[platform/upstream/isl.git] / isl_dim.c
index 841b291..6aa67f0 100644 (file)
--- a/isl_dim.c
+++ b/isl_dim.c
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <isl_dim_private.h>
 #include "isl_name.h"
+#include <isl_reordering.h>
 
 isl_ctx *isl_dim_get_ctx(__isl_keep isl_dim *dim)
 {
@@ -168,6 +169,7 @@ static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
        case isl_dim_param:     return dim->nparam;
        case isl_dim_in:        return dim->n_in;
        case isl_dim_out:       return dim->n_out;
+       case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
        default:                return 0;
        }
 }
@@ -508,13 +510,27 @@ struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
        case isl_dim_out:
                return isl_dim_extend(dim,
                                        dim->nparam, dim->n_in, dim->n_out + n);
+       default:
+               isl_die(dim->ctx, isl_error_invalid,
+                       "cannot add dimensions of specified type", goto error);
        }
-       return dim;
 error:
        isl_dim_free(dim);
        return NULL;
 }
 
+static int valid_dim_type(enum isl_dim_type type)
+{
+       switch (type) {
+       case isl_dim_param:
+       case isl_dim_in:
+       case isl_dim_out:
+               return 1;
+       default:
+               return 0;
+       }
+}
+
 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
        enum isl_dim_type type, unsigned pos, unsigned n)
 {
@@ -525,6 +541,11 @@ __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
        if (n == 0)
                return isl_dim_reset(dim, type);
 
+       if (!valid_dim_type(type))
+               isl_die(dim->ctx, isl_error_invalid,
+                       "cannot insert dimensions of specified type",
+                       goto error);
+
        isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
 
        dim = isl_dim_cow(dim);
@@ -563,6 +584,7 @@ __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
        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;
+       default:                ;
        }
        dim = isl_dim_reset(dim, type);
 
@@ -641,12 +663,14 @@ __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
        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;
+       default:                ;
        }
 
        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;
+       default:                ;
        }
 
        if (dst_type != isl_dim_param && src_type != isl_dim_param)
@@ -869,9 +893,13 @@ struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
        if (!dim)
                return NULL;
 
-       if (n == 0)
+       if (num == 0)
                return isl_dim_reset(dim, type);
 
+       if (!valid_dim_type(type))
+               isl_die(dim->ctx, isl_error_invalid,
+                       "cannot drop dimensions of specified type", goto error);
+
        isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
        dim = isl_dim_cow(dim);
        if (!dim)
@@ -891,7 +919,7 @@ struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
                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:
+               default:
                        ;
                }
                dim->n_name -= num;
@@ -900,6 +928,7 @@ struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type 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;
+       default:                ;
        }
        dim = isl_dim_reset(dim, type);
        if (type == isl_dim_param) {
@@ -1125,6 +1154,17 @@ int isl_dim_is_named_or_nested(__isl_keep isl_dim *dim, enum isl_dim_type type)
        return 0;
 }
 
+int isl_dim_may_be_set(__isl_keep isl_dim *dim)
+{
+       if (!dim)
+               return -1;
+       if (isl_dim_size(dim, isl_dim_in) != 0)
+               return 0;
+       if (isl_dim_is_named_or_nested(dim, isl_dim_in))
+               return 0;
+       return 1;
+}
+
 __isl_give isl_dim *isl_dim_reset(__isl_take isl_dim *dim,
        enum isl_dim_type type)
 {
@@ -1158,6 +1198,16 @@ __isl_give isl_dim *isl_dim_flatten(__isl_take isl_dim *dim)
        return dim;
 }
 
+__isl_give isl_dim *isl_dim_flatten_range(__isl_take isl_dim *dim)
+{
+       if (!dim)
+               return NULL;
+       if (!dim->nested[1])
+               return dim;
+
+       return isl_dim_reset(dim, isl_dim_out);
+}
+
 /* Replace the dimensions of the given type of dst by those of src.
  */
 __isl_give isl_dim *isl_dim_replace(__isl_take isl_dim *dst,
@@ -1249,3 +1299,47 @@ error:
        isl_dim_free(dim);
        return NULL;
 }
+
+int isl_dim_has_named_params(__isl_keep isl_dim *dim)
+{
+       int i;
+       unsigned off;
+
+       if (!dim)
+               return -1;
+       if (dim->nparam == 0)
+               return 1;
+       off = isl_dim_offset(dim, isl_dim_param);
+       if (off + dim->nparam > dim->n_name)
+               return 0;
+       for (i = 0; i < dim->nparam; ++i)
+               if (!dim->names[off + i])
+                       return 0;
+       return 1;
+}
+
+/* Align the initial parameters of dim1 to match the order in dim2.
+ */
+__isl_give isl_dim *isl_dim_align_params(__isl_take isl_dim *dim1,
+       __isl_take isl_dim *dim2)
+{
+       isl_reordering *exp;
+
+       if (!isl_dim_has_named_params(dim1) || !isl_dim_has_named_params(dim2))
+               isl_die(isl_dim_get_ctx(dim1), isl_error_invalid,
+                       "parameter alignment requires named parameters",
+                       goto error);
+
+       exp = isl_parameter_alignment_reordering(dim1, dim2);
+       isl_dim_free(dim1);
+       isl_dim_free(dim2);
+       if (!exp)
+               return NULL;
+       dim1 = isl_dim_copy(exp->dim);
+       isl_reordering_free(exp);
+       return dim1;
+error:
+       isl_dim_free(dim1);
+       isl_dim_free(dim2);
+       return NULL;
+}