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, return isl_dim_total(dim));
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 offset(struct isl_dim *dim, enum isl_dim_type type)
112 case isl_dim_param: return 0;
113 case isl_dim_in: return dim->nparam;
114 case isl_dim_out: return dim->nparam + dim->n_in;
118 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
121 case isl_dim_param: return dim->nparam;
122 case isl_dim_in: return dim->n_in;
123 case isl_dim_out: return dim->n_out;
127 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
134 static struct isl_dim *copy_names(struct isl_dim *dst,
135 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
136 enum isl_dim_type src_type)
139 struct isl_name *name;
141 for (i = 0; i < n(src, src_type); ++i) {
142 name = get_name(src, src_type, i);
145 dst = set_name(dst, dst_type, offset + i,
146 isl_name_copy(dst->ctx, name));
153 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
156 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
159 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
160 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
161 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
165 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
173 return isl_dim_dup(dim);
176 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
185 void isl_dim_free(struct isl_dim *dim)
195 for (i = 0; i < dim->n_name; ++i)
196 isl_name_free(dim->ctx, dim->names[i]);
198 isl_ctx_deref(dim->ctx);
203 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
204 enum isl_dim_type type, unsigned pos,
207 struct isl_name *name;
210 name = isl_name_get(dim->ctx, s);
213 return set_name(dim, type, pos, name);
219 const char *isl_dim_get_name(struct isl_dim *dim,
220 enum isl_dim_type type, unsigned pos)
222 struct isl_name *name = get_name(dim, type, pos);
223 return name ? name->name : NULL;
226 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
227 struct isl_dim *dim2, enum isl_dim_type dim2_type)
231 if (n(dim1, dim1_type) != n(dim2, dim2_type))
234 if (!dim1->names && !dim2->names)
237 for (i = 0; i < n(dim1, dim1_type); ++i) {
238 if (get_name(dim1, dim1_type, i) !=
239 get_name(dim2, dim2_type, i))
245 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
246 struct isl_dim *dim2, enum isl_dim_type dim2_type)
248 return match(dim1, dim1_type, dim2, dim2_type);
251 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
252 unsigned first, unsigned n, struct isl_name **names)
256 for (i = 0; i < n ; ++i)
257 names[i] = get_name(dim, type, first+i);
260 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
261 unsigned nparam, unsigned n_in, unsigned n_out)
263 struct isl_name **names = NULL;
267 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
270 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
271 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
272 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
274 dim = isl_dim_cow(dim);
277 names = isl_calloc_array(dim->ctx, struct isl_name *,
278 nparam + n_in + n_out);
281 get_names(dim, isl_dim_param, 0, dim->nparam, names);
282 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
283 get_names(dim, isl_dim_out, 0, dim->n_out,
284 names + nparam + n_in);
287 dim->n_name = nparam + n_in + n_out;
289 dim->nparam = nparam;
300 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
305 return isl_dim_extend(dim,
306 dim->nparam + n, dim->n_in, dim->n_out);
308 return isl_dim_extend(dim,
309 dim->nparam, dim->n_in + n, dim->n_out);
311 return isl_dim_extend(dim,
312 dim->nparam, dim->n_in, dim->n_out + n);
317 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
324 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
326 isl_assert(left->ctx, match(left, isl_dim_out, right, isl_dim_in),
329 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
333 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
334 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
335 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
347 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
354 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
357 dim = isl_dim_alloc(left->ctx, left->nparam,
358 left->n_in + right->n_in, left->n_out + right->n_out);
362 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
363 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
364 dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
365 dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
366 dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
378 struct isl_dim *isl_dim_map(struct isl_dim *dim)
380 struct isl_name **names = NULL;
384 isl_assert(dim->ctx, dim->n_in == 0, goto error);
387 dim = isl_dim_cow(dim);
391 names = isl_calloc_array(dim->ctx, struct isl_name *,
392 dim->nparam + dim->n_out + dim->n_out);
395 get_names(dim, isl_dim_param, 0, dim->nparam, names);
396 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
398 dim->n_in = dim->n_out;
400 copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
403 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
411 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
412 unsigned first, unsigned n, struct isl_name **names)
416 for (i = 0; i < n ; ++i)
417 dim = set_name(dim, type, first+i, names[i]);
422 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
425 struct isl_name **names = NULL;
429 if (match(dim, isl_dim_in, dim, isl_dim_out))
432 dim = isl_dim_cow(dim);
437 names = isl_alloc_array(dim->ctx, struct isl_name *,
438 dim->n_in + dim->n_out);
441 get_names(dim, isl_dim_in, 0, dim->n_in, names);
442 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
446 dim->n_in = dim->n_out;
450 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
451 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
462 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
463 unsigned first, unsigned num)
473 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
474 dim = isl_dim_cow(dim);
478 for (i = 0; i < num; ++i)
479 isl_name_free(dim->ctx, get_name(dim, type, first+i));
480 for (i = first+num; i < n(dim, type); ++i)
481 set_name(dim, type, i - num, get_name(dim, type, i));
484 get_names(dim, isl_dim_in, 0, dim->n_in,
485 dim->names + offset(dim, isl_dim_in) - num);
487 get_names(dim, isl_dim_out, 0, dim->n_out,
488 dim->names + offset(dim, isl_dim_out) - num);
494 case isl_dim_param: dim->nparam -= num; break;
495 case isl_dim_in: dim->n_in -= num; break;
496 case isl_dim_out: dim->n_out -= num; break;
504 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
505 unsigned first, unsigned n)
507 return isl_dim_drop(dim, isl_dim_in, first, n);
510 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
511 unsigned first, unsigned n)
513 return isl_dim_drop(dim, isl_dim_out, first, n);
516 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
520 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
521 return isl_dim_reverse(dim);
524 struct isl_dim *isl_dim_range(struct isl_dim *dim)
528 return isl_dim_drop_inputs(dim, 0, dim->n_in);
531 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
538 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
540 dim = isl_dim_cow(dim);
543 dim->n_out += dim->nparam + dim->n_in + n_div;
547 for (i = 0; i < dim->n_name; ++i)
548 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
554 unsigned isl_dim_total(struct isl_dim *dim)
556 return dim->nparam + dim->n_in + dim->n_out;
559 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
561 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
562 match(dim1, isl_dim_in, dim2, isl_dim_in) &&
563 match(dim1, isl_dim_out, dim2, isl_dim_out);
566 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
568 return dim1->nparam == dim2->nparam &&
569 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;