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)
83 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 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
123 static struct isl_dim *copy_names(struct isl_dim *dst,
124 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
125 enum isl_dim_type src_type)
128 struct isl_name *name;
130 for (i = 0; i < n(src, src_type); ++i) {
131 name = get_name(src, src_type, i);
134 dst = set_name(dst, dst_type, offset + i,
135 isl_name_copy(dst->ctx, name));
142 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
145 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
148 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
149 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
150 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
154 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
162 return isl_dim_dup(dim);
165 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
174 void isl_dim_free(struct isl_dim *dim)
184 for (i = 0; i < dim->n_name; ++i)
185 isl_name_free(dim->ctx, dim->names[i]);
187 isl_ctx_deref(dim->ctx);
192 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
193 enum isl_dim_type type, unsigned pos,
196 struct isl_name *name;
199 name = isl_name_get(dim->ctx, s);
202 return set_name(dim, type, pos, name);
208 const char *isl_dim_get_name(struct isl_dim *dim,
209 enum isl_dim_type type, unsigned pos)
211 struct isl_name *name = get_name(dim, type, pos);
212 return name ? name->name : NULL;
215 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
216 struct isl_dim *dim2, enum isl_dim_type dim2_type)
220 if (n(dim1, dim1_type) != n(dim2, dim2_type))
223 if (!dim1->names && !dim2->names)
226 for (i = 0; i < n(dim1, dim1_type); ++i) {
227 if (get_name(dim1, dim1_type, i) !=
228 get_name(dim2, dim2_type, i))
234 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
235 struct isl_dim *dim2, enum isl_dim_type dim2_type)
237 return match(dim1, dim1_type, dim2, dim2_type);
240 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
241 unsigned first, unsigned n, struct isl_name **names)
245 for (i = 0; i < n ; ++i)
246 names[i] = get_name(dim, type, first+i);
249 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
250 unsigned nparam, unsigned n_in, unsigned n_out)
252 struct isl_name **names = NULL;
256 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
259 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
260 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
261 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
263 dim = isl_dim_cow(dim);
266 names = isl_calloc_array(dim->ctx, struct isl_name *,
267 nparam + n_in + n_out);
270 get_names(dim, isl_dim_param, 0, dim->nparam, names);
271 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
272 get_names(dim, isl_dim_out, 0, dim->n_out,
273 names + nparam + n_in);
276 dim->n_name = nparam + n_in + n_out;
278 dim->nparam = nparam;
289 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
294 return isl_dim_extend(dim,
295 dim->nparam + n, dim->n_in, dim->n_out);
297 return isl_dim_extend(dim,
298 dim->nparam, dim->n_in + n, dim->n_out);
300 return isl_dim_extend(dim,
301 dim->nparam, dim->n_in, dim->n_out + n);
306 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
313 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
315 isl_assert(left->ctx, match(left, isl_dim_out, right, isl_dim_in),
318 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
322 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
323 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
324 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
336 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
343 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
346 dim = isl_dim_alloc(left->ctx, left->nparam,
347 left->n_in + right->n_in, left->n_out + right->n_out);
351 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
352 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
353 dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
354 dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
355 dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
367 struct isl_dim *isl_dim_map(struct isl_dim *dim)
369 struct isl_name **names = NULL;
373 isl_assert(dim->ctx, dim->n_in == 0, goto error);
376 dim = isl_dim_cow(dim);
380 names = isl_calloc_array(dim->ctx, struct isl_name *,
381 dim->nparam + dim->n_out + dim->n_out);
384 get_names(dim, isl_dim_param, 0, dim->nparam, names);
385 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
387 dim->n_in = dim->n_out;
389 copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
392 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
400 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
401 unsigned first, unsigned n, struct isl_name **names)
405 for (i = 0; i < n ; ++i)
406 dim = set_name(dim, type, first+i, names[i]);
411 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
414 struct isl_name **names = NULL;
418 if (match(dim, isl_dim_in, dim, isl_dim_out))
421 dim = isl_dim_cow(dim);
426 names = isl_alloc_array(dim->ctx, struct isl_name *,
427 dim->n_in + dim->n_out);
430 get_names(dim, isl_dim_in, 0, dim->n_in, names);
431 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
435 dim->n_in = dim->n_out;
439 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
440 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
451 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
452 unsigned first, unsigned n)
462 isl_assert(dim->ctx, first + n <= dim->n_in, goto error);
463 dim = isl_dim_cow(dim);
467 for (i = 0; i < n; ++i) {
468 isl_name_free(dim->ctx,
469 get_name(dim, isl_dim_in, first+i));
471 for (i = first+n; i < dim->n_in; ++i)
472 set_name(dim, isl_dim_in, i - n,
473 get_name(dim, isl_dim_in, i));
474 get_names(dim, isl_dim_out, 0, dim->n_out,
475 dim->names + dim->nparam + dim->n_in - n);
484 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
485 unsigned first, unsigned n)
495 isl_assert(dim->ctx, first + n <= dim->n_out, goto error);
496 dim = isl_dim_cow(dim);
500 for (i = 0; i < n; ++i) {
501 isl_name_free(dim->ctx,
502 get_name(dim, isl_dim_out, first+i));
504 for (i = first+n; i < dim->n_out; ++i)
505 set_name(dim, isl_dim_out, i - n,
506 get_name(dim, isl_dim_out, i));
515 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
519 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
520 return isl_dim_reverse(dim);
523 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
530 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
532 dim = isl_dim_cow(dim);
535 dim->n_out += dim->nparam + dim->n_in + n_div;
539 for (i = 0; i < dim->n_name; ++i)
540 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
546 unsigned isl_dim_total(struct isl_dim *dim)
548 return dim->nparam + dim->n_in + dim->n_out;
551 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
553 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
554 match(dim1, isl_dim_in, dim2, isl_dim_in) &&
555 match(dim1, isl_dim_out, dim2, isl_dim_out);
558 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
560 return dim1->nparam == dim2->nparam &&
561 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;