4 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
5 unsigned nparam, unsigned n_in, unsigned n_out)
9 dim = isl_alloc_type(ctx, struct isl_dim);
26 struct isl_dim *isl_dim_set_alloc(struct isl_ctx *ctx,
27 unsigned nparam, unsigned dim)
29 return isl_dim_alloc(ctx, nparam, 0, dim);
32 static unsigned global_pos(struct isl_dim *dim,
33 enum isl_dim_type type, unsigned pos)
35 struct isl_ctx *ctx = dim->ctx;
39 isl_assert(ctx, pos < dim->nparam, return isl_dim_total(dim));
42 isl_assert(ctx, pos < dim->n_in, return isl_dim_total(dim));
43 return pos + dim->nparam;
45 isl_assert(ctx, pos < dim->n_out, return isl_dim_total(dim));
46 return pos + dim->nparam + dim->n_in;
48 isl_assert(ctx, 0, goto error);
50 return isl_dim_total(dim);
53 static struct isl_dim *set_name(struct isl_dim *dim,
54 enum isl_dim_type type, unsigned pos,
55 struct isl_name *name)
57 struct isl_ctx *ctx = dim->ctx;
58 dim = isl_dim_cow(dim);
63 pos = global_pos(dim, type, pos);
64 isl_assert(ctx, pos != isl_dim_total(dim), goto error);
66 if (pos >= dim->n_name) {
70 dim->names = isl_calloc_array(dim->ctx,
71 struct isl_name *, isl_dim_total(dim));
76 dim->names = isl_realloc_array(dim->ctx, dim->names,
77 struct isl_name *, isl_dim_total(dim));
80 for (i = dim->n_name; i < isl_dim_total(dim); ++i)
84 dim->n_name = isl_dim_total(dim);
86 dim->names[pos] = name;
90 isl_name_free(ctx, name);
95 static struct isl_name *get_name(struct isl_dim *dim,
96 enum isl_dim_type type, unsigned pos)
101 pos = global_pos(dim, type, pos);
102 if (pos == isl_dim_total(dim))
104 if (pos >= dim->n_name)
106 return dim->names[pos];
109 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
112 case isl_dim_param: return dim->nparam;
113 case isl_dim_in: return dim->n_in;
114 case isl_dim_out: return dim->n_out;
118 static struct isl_dim *copy_names(struct isl_dim *dst,
119 enum isl_dim_type dst_type, struct isl_dim *src,
120 enum isl_dim_type src_type)
123 struct isl_name *name;
125 for (i = 0; i < n(dst, dst_type); ++i) {
126 name = get_name(src, src_type, i);
129 dst = set_name(dst, dst_type, i, isl_name_copy(dst->ctx, name));
136 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
139 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
142 dup = copy_names(dup, isl_dim_param, dim, isl_dim_param);
143 dup = copy_names(dup, isl_dim_in, dim, isl_dim_in);
144 dup = copy_names(dup, isl_dim_out, dim, isl_dim_out);
148 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
156 return isl_dim_dup(dim);
159 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
168 void isl_dim_free(struct isl_dim *dim)
178 for (i = 0; i < dim->n_name; ++i)
179 isl_name_free(dim->ctx, dim->names[i]);
181 isl_ctx_deref(dim->ctx);
186 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
187 enum isl_dim_type type, unsigned pos,
190 struct isl_name *name;
193 name = isl_name_get(dim->ctx, s);
196 return set_name(dim, type, pos, name);
202 const char *isl_dim_get_name(struct isl_dim *dim,
203 enum isl_dim_type type, unsigned pos)
205 struct isl_name *name = get_name(dim, type, pos);
206 return name ? name->name : NULL;
209 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
210 struct isl_dim *dim2, enum isl_dim_type dim2_type)
214 if (n(dim1, dim1_type) != n(dim2, dim2_type))
217 if (!dim1->names && !dim2->names)
220 for (i = 0; i < n(dim1, dim1_type); ++i) {
221 if (get_name(dim1, dim1_type, i) !=
222 get_name(dim2, dim2_type, i))
228 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
229 unsigned first, unsigned n, struct isl_name **names)
233 for (i = 0; i < n ; ++i)
234 names[i] = get_name(dim, type, first+i);
237 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
238 unsigned nparam, unsigned n_in, unsigned n_out)
240 struct isl_name **names = NULL;
244 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
247 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
248 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
249 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
251 dim = isl_dim_cow(dim);
254 names = isl_calloc_array(dim->ctx, struct isl_name *,
255 nparam + n_in + n_out);
258 get_names(dim, isl_dim_param, 0, dim->nparam, names);
259 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
260 get_names(dim, isl_dim_out, 0, dim->n_out,
261 names + nparam + n_in);
265 dim->nparam = nparam;
276 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
283 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
285 isl_assert(left->ctx, match(left, isl_dim_out, right, isl_dim_in),
288 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
292 dim = copy_names(dim, isl_dim_param, left, isl_dim_param);
293 dim = copy_names(dim, isl_dim_in, left, isl_dim_in);
294 dim = copy_names(dim, isl_dim_out, right, isl_dim_out);
306 struct isl_dim *isl_dim_map(struct isl_dim *dim)
308 struct isl_name **names = NULL;
312 isl_assert(dim->ctx, dim->n_in == 0, goto error);
315 dim = isl_dim_cow(dim);
319 names = isl_calloc_array(dim->ctx, struct isl_name *,
320 dim->nparam + dim->n_out + dim->n_out);
323 get_names(dim, isl_dim_param, 0, dim->nparam, names);
324 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
326 dim->n_in = dim->n_out;
328 copy_names(dim, isl_dim_out, dim, isl_dim_in);
338 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
339 unsigned first, unsigned n, struct isl_name **names)
343 for (i = 0; i < n ; ++i)
344 dim = set_name(dim, type, first+i, names[i]);
349 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
352 struct isl_name **names = NULL;
356 if (match(dim, isl_dim_in, dim, isl_dim_out))
359 dim = isl_dim_cow(dim);
364 names = isl_alloc_array(dim->ctx, struct isl_name *,
365 dim->n_in + dim->n_out);
368 get_names(dim, isl_dim_in, 0, dim->n_in, names);
369 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
373 dim->n_in = dim->n_out;
377 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
378 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
389 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
390 unsigned first, unsigned n)
400 isl_assert(dim->ctx, first + n <= dim->n_in, goto error);
401 dim = isl_dim_cow(dim);
405 for (i = 0; i < n; ++i) {
406 isl_name_free(dim->ctx,
407 get_name(dim, isl_dim_in, first+i));
409 for (i = first+n; i < dim->n_in; ++i)
410 set_name(dim, isl_dim_in, i - n,
411 get_name(dim, isl_dim_in, i));
412 get_names(dim, isl_dim_out, 0, dim->n_out,
413 dim->names + dim->nparam + dim->n_in - n);
422 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
423 unsigned first, unsigned n)
433 isl_assert(dim->ctx, first + n <= dim->n_out, goto error);
434 dim = isl_dim_cow(dim);
438 for (i = 0; i < n; ++i) {
439 isl_name_free(dim->ctx,
440 get_name(dim, isl_dim_out, first+i));
442 for (i = first+n; i < dim->n_out; ++i)
443 set_name(dim, isl_dim_out, i - n,
444 get_name(dim, isl_dim_out, i));
453 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
457 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
458 return isl_dim_reverse(dim);
461 unsigned isl_dim_total(struct isl_dim *dim)
463 return dim->nparam + dim->n_in + dim->n_out;
466 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
468 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
469 match(dim1, isl_dim_in, dim2, isl_dim_in) &&
470 match(dim1, isl_dim_out, dim2, isl_dim_out);
473 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
475 return dim1->nparam == dim2->nparam &&
476 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;