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 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)
132 static struct isl_dim *copy_names(struct isl_dim *dst,
133 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
134 enum isl_dim_type src_type)
137 struct isl_name *name;
139 for (i = 0; i < n(src, src_type); ++i) {
140 name = get_name(src, src_type, i);
143 dst = set_name(dst, dst_type, offset + i,
144 isl_name_copy(dst->ctx, name));
151 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
154 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
157 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
158 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
159 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
163 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
171 return isl_dim_dup(dim);
174 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
183 void isl_dim_free(struct isl_dim *dim)
193 for (i = 0; i < dim->n_name; ++i)
194 isl_name_free(dim->ctx, dim->names[i]);
196 isl_ctx_deref(dim->ctx);
201 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
202 enum isl_dim_type type, unsigned pos,
205 struct isl_name *name;
208 name = isl_name_get(dim->ctx, s);
211 return set_name(dim, type, pos, name);
217 const char *isl_dim_get_name(struct isl_dim *dim,
218 enum isl_dim_type type, unsigned pos)
220 struct isl_name *name = get_name(dim, type, pos);
221 return name ? name->name : NULL;
224 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
225 struct isl_dim *dim2, enum isl_dim_type dim2_type)
229 if (n(dim1, dim1_type) != n(dim2, dim2_type))
232 if (!dim1->names && !dim2->names)
235 for (i = 0; i < n(dim1, dim1_type); ++i) {
236 if (get_name(dim1, dim1_type, i) !=
237 get_name(dim2, dim2_type, i))
243 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
244 struct isl_dim *dim2, enum isl_dim_type dim2_type)
246 return match(dim1, dim1_type, dim2, dim2_type);
249 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
250 unsigned first, unsigned n, struct isl_name **names)
254 for (i = 0; i < n ; ++i)
255 names[i] = get_name(dim, type, first+i);
258 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
259 unsigned nparam, unsigned n_in, unsigned n_out)
261 struct isl_name **names = NULL;
265 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
268 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
269 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
270 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
272 dim = isl_dim_cow(dim);
275 names = isl_calloc_array(dim->ctx, struct isl_name *,
276 nparam + n_in + n_out);
279 get_names(dim, isl_dim_param, 0, dim->nparam, names);
280 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
281 get_names(dim, isl_dim_out, 0, dim->n_out,
282 names + nparam + n_in);
285 dim->n_name = nparam + n_in + n_out;
287 dim->nparam = nparam;
298 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
303 return isl_dim_extend(dim,
304 dim->nparam + n, dim->n_in, dim->n_out);
306 return isl_dim_extend(dim,
307 dim->nparam, dim->n_in + n, dim->n_out);
309 return isl_dim_extend(dim,
310 dim->nparam, dim->n_in, dim->n_out + n);
315 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
322 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
324 isl_assert(left->ctx, match(left, isl_dim_out, right, isl_dim_in),
327 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
331 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
332 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
333 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
345 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
352 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
355 dim = isl_dim_alloc(left->ctx, left->nparam,
356 left->n_in + right->n_in, left->n_out + right->n_out);
360 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
361 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
362 dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
363 dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
364 dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
376 struct isl_dim *isl_dim_map(struct isl_dim *dim)
378 struct isl_name **names = NULL;
382 isl_assert(dim->ctx, dim->n_in == 0, goto error);
385 dim = isl_dim_cow(dim);
389 names = isl_calloc_array(dim->ctx, struct isl_name *,
390 dim->nparam + dim->n_out + dim->n_out);
393 get_names(dim, isl_dim_param, 0, dim->nparam, names);
394 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
396 dim->n_in = dim->n_out;
398 copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
401 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
409 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
410 unsigned first, unsigned n, struct isl_name **names)
414 for (i = 0; i < n ; ++i)
415 dim = set_name(dim, type, first+i, names[i]);
420 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
423 struct isl_name **names = NULL;
427 if (match(dim, isl_dim_in, dim, isl_dim_out))
430 dim = isl_dim_cow(dim);
435 names = isl_alloc_array(dim->ctx, struct isl_name *,
436 dim->n_in + dim->n_out);
439 get_names(dim, isl_dim_in, 0, dim->n_in, names);
440 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
444 dim->n_in = dim->n_out;
448 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
449 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
460 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
461 unsigned first, unsigned num)
471 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
472 dim = isl_dim_cow(dim);
476 for (i = 0; i < num; ++i)
477 isl_name_free(dim->ctx, get_name(dim, type, first+i));
478 for (i = first+num; i < n(dim, type); ++i)
479 set_name(dim, type, i - num, get_name(dim, type, i));
482 get_names(dim, isl_dim_in, 0, dim->n_in,
483 dim->names + offset(dim, isl_dim_in) - num);
485 get_names(dim, isl_dim_out, 0, dim->n_out,
486 dim->names + offset(dim, isl_dim_out) - num);
492 case isl_dim_param: dim->nparam -= num; break;
493 case isl_dim_in: dim->n_in -= num; break;
494 case isl_dim_out: dim->n_out -= num; break;
502 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
503 unsigned first, unsigned n)
505 return isl_dim_drop(dim, isl_dim_in, first, n);
508 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
509 unsigned first, unsigned n)
511 return isl_dim_drop(dim, isl_dim_out, first, n);
514 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
518 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
519 return isl_dim_reverse(dim);
522 struct isl_dim *isl_dim_range(struct isl_dim *dim)
526 return isl_dim_drop_inputs(dim, 0, dim->n_in);
529 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
536 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
538 dim = isl_dim_cow(dim);
541 dim->n_out += dim->nparam + dim->n_in + n_div;
545 for (i = 0; i < dim->n_name; ++i)
546 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
552 unsigned isl_dim_total(struct isl_dim *dim)
554 return dim->nparam + dim->n_in + dim->n_out;
557 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
559 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
560 match(dim1, isl_dim_in, dim2, isl_dim_in) &&
561 match(dim1, isl_dim_out, dim2, isl_dim_out);
564 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
566 return dim1->nparam == dim2->nparam &&
567 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;