2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include <isl_dim_private.h>
14 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
15 unsigned nparam, unsigned n_in, unsigned n_out)
19 dim = isl_alloc_type(ctx, struct isl_dim);
30 dim->tuple_name[0] = NULL;
31 dim->tuple_name[1] = NULL;
39 struct isl_dim *isl_dim_set_alloc(struct isl_ctx *ctx,
40 unsigned nparam, unsigned dim)
42 return isl_dim_alloc(ctx, nparam, 0, dim);
45 static unsigned global_pos(struct isl_dim *dim,
46 enum isl_dim_type type, unsigned pos)
48 struct isl_ctx *ctx = dim->ctx;
52 isl_assert(ctx, pos < dim->nparam, return isl_dim_total(dim));
55 isl_assert(ctx, pos < dim->n_in, return isl_dim_total(dim));
56 return pos + dim->nparam;
58 isl_assert(ctx, pos < dim->n_out, return isl_dim_total(dim));
59 return pos + dim->nparam + dim->n_in;
61 isl_assert(ctx, 0, return isl_dim_total(dim));
63 return isl_dim_total(dim);
66 /* Extend length of names array to the total number of dimensions.
68 static __isl_give isl_dim *extend_names(__isl_take isl_dim *dim)
70 struct isl_name **names;
73 if (isl_dim_total(dim) <= dim->n_name)
77 dim->names = isl_calloc_array(dim->ctx,
78 struct isl_name *, isl_dim_total(dim));
82 names = isl_realloc_array(dim->ctx, dim->names,
83 struct isl_name *, isl_dim_total(dim));
87 for (i = dim->n_name; i < isl_dim_total(dim); ++i)
91 dim->n_name = isl_dim_total(dim);
99 static struct isl_dim *set_name(struct isl_dim *dim,
100 enum isl_dim_type type, unsigned pos,
101 struct isl_name *name)
103 struct isl_ctx *ctx = dim->ctx;
104 dim = isl_dim_cow(dim);
109 pos = global_pos(dim, type, pos);
110 isl_assert(ctx, pos != isl_dim_total(dim), goto error);
112 if (pos >= dim->n_name) {
115 dim = extend_names(dim);
120 dim->names[pos] = name;
124 isl_name_free(ctx, name);
129 static struct isl_name *get_name(struct isl_dim *dim,
130 enum isl_dim_type type, unsigned pos)
135 pos = global_pos(dim, type, pos);
136 if (pos == isl_dim_total(dim))
138 if (pos >= dim->n_name)
140 return dim->names[pos];
143 static unsigned offset(struct isl_dim *dim, enum isl_dim_type type)
146 case isl_dim_param: return 0;
147 case isl_dim_in: return dim->nparam;
148 case isl_dim_out: return dim->nparam + dim->n_in;
153 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
156 case isl_dim_param: return dim->nparam;
157 case isl_dim_in: return dim->n_in;
158 case isl_dim_out: return dim->n_out;
163 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
170 unsigned isl_dim_offset(__isl_keep isl_dim *dim, enum isl_dim_type type)
174 return offset(dim, type);
177 static struct isl_dim *copy_names(struct isl_dim *dst,
178 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
179 enum isl_dim_type src_type)
182 struct isl_name *name;
187 for (i = 0; i < n(src, src_type); ++i) {
188 name = get_name(src, src_type, i);
191 dst = set_name(dst, dst_type, offset + i,
192 isl_name_copy(dst->ctx, name));
199 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
204 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
205 if (dim->tuple_name[0] &&
206 !(dup->tuple_name[0] = isl_name_copy(dim->ctx, dim->tuple_name[0])))
208 if (dim->tuple_name[1] &&
209 !(dup->tuple_name[1] = isl_name_copy(dim->ctx, dim->tuple_name[1])))
213 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
214 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
215 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
222 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
230 return isl_dim_dup(dim);
233 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
242 void isl_dim_free(struct isl_dim *dim)
252 isl_name_free(dim->ctx, dim->tuple_name[0]);
253 isl_name_free(dim->ctx, dim->tuple_name[1]);
255 for (i = 0; i < dim->n_name; ++i)
256 isl_name_free(dim->ctx, dim->names[i]);
258 isl_ctx_deref(dim->ctx);
263 static int name_ok(isl_ctx *ctx, const char *s)
268 dummy = strtol(s, &p, 0);
270 isl_die(ctx, isl_error_invalid, "name looks like a number",
276 __isl_give isl_dim *isl_dim_set_tuple_name(__isl_take isl_dim *dim,
277 enum isl_dim_type type, const char *s)
279 struct isl_name *name;
281 dim = isl_dim_cow(dim);
284 if (type != isl_dim_in && type != isl_dim_out)
285 isl_die(dim->ctx, isl_error_invalid,
286 "only input, output and set tuples can have names",
291 if (!name_ok(dim->ctx, s))
293 name = isl_name_get(dim->ctx, s);
298 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
299 dim->tuple_name[type - isl_dim_in] = name;
307 const char *isl_dim_get_tuple_name(__isl_keep isl_dim *dim,
308 enum isl_dim_type type)
310 struct isl_name *name;
313 if (type != isl_dim_in && type != isl_dim_out)
315 name = dim->tuple_name[type - isl_dim_in];
316 return name ? name->name : NULL;
319 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
320 enum isl_dim_type type, unsigned pos,
323 struct isl_name *name;
327 if (!name_ok(dim->ctx, s))
329 name = isl_name_get(dim->ctx, s);
332 return set_name(dim, type, pos, name);
338 const char *isl_dim_get_name(struct isl_dim *dim,
339 enum isl_dim_type type, unsigned pos)
341 struct isl_name *name = get_name(dim, type, pos);
342 return name ? name->name : NULL;
345 static struct isl_name *tuple_name(__isl_keep isl_dim *dim,
346 enum isl_dim_type type)
350 if (type == isl_dim_in)
351 return dim->tuple_name[0];
352 if (type == isl_dim_out)
353 return dim->tuple_name[1];
357 int isl_dim_tuple_match(__isl_keep isl_dim *dim1, enum isl_dim_type dim1_type,
358 __isl_keep isl_dim *dim2, enum isl_dim_type dim2_type)
360 struct isl_name *name1, *name2;
362 if (n(dim1, dim1_type) != n(dim2, dim2_type))
364 name1 = tuple_name(dim1, dim1_type);
365 name2 = tuple_name(dim2, dim2_type);
368 if (name1 && name1->name != name2->name)
373 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
374 struct isl_dim *dim2, enum isl_dim_type dim2_type)
378 if (!isl_dim_tuple_match(dim1, dim1_type, dim2, dim2_type))
381 if (!dim1->names && !dim2->names)
384 for (i = 0; i < n(dim1, dim1_type); ++i) {
385 if (get_name(dim1, dim1_type, i) !=
386 get_name(dim2, dim2_type, i))
392 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
393 struct isl_dim *dim2, enum isl_dim_type dim2_type)
395 return match(dim1, dim1_type, dim2, dim2_type);
398 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
399 unsigned first, unsigned n, struct isl_name **names)
403 for (i = 0; i < n ; ++i)
404 names[i] = get_name(dim, type, first+i);
407 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
408 unsigned nparam, unsigned n_in, unsigned n_out)
410 struct isl_name **names = NULL;
414 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
417 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
418 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
419 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
421 dim = isl_dim_cow(dim);
424 names = isl_calloc_array(dim->ctx, struct isl_name *,
425 nparam + n_in + n_out);
428 get_names(dim, isl_dim_param, 0, dim->nparam, names);
429 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
430 get_names(dim, isl_dim_out, 0, dim->n_out,
431 names + nparam + n_in);
434 dim->n_name = nparam + n_in + n_out;
436 dim->nparam = nparam;
447 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
452 if ((type == isl_dim_in || type == isl_dim_out) &&
453 dim->tuple_name[type - isl_dim_in]) {
454 dim = isl_dim_cow(dim);
457 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
458 dim->tuple_name[type - isl_dim_in] = NULL;
462 return isl_dim_extend(dim,
463 dim->nparam + n, dim->n_in, dim->n_out);
465 return isl_dim_extend(dim,
466 dim->nparam, dim->n_in + n, dim->n_out);
468 return isl_dim_extend(dim,
469 dim->nparam, dim->n_in, dim->n_out + n);
474 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
475 enum isl_dim_type type, unsigned pos, unsigned n)
477 struct isl_name **names = NULL;
481 if (n == 0 && !isl_dim_get_tuple_name(dim, type))
484 isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
486 dim = isl_dim_cow(dim);
494 names = isl_calloc_array(dim->ctx, struct isl_name *,
495 dim->nparam + dim->n_in + dim->n_out + n);
499 size[isl_dim_param] = dim->nparam;
500 size[isl_dim_in] = dim->n_in;
501 size[isl_dim_out] = dim->n_out;
502 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
504 get_names(dim, t, 0, size[t], names + off);
507 get_names(dim, t, 0, pos, names + off);
509 get_names(dim, t, pos, size[t]-pos, names+off);
510 off += size[t] - pos;
515 dim->n_name = dim->nparam + dim->n_in + dim->n_out + n;
518 case isl_dim_param: dim->nparam += n; break;
519 case isl_dim_in: dim->n_in += n; break;
520 case isl_dim_out: dim->n_out += n; break;
522 if (type == isl_dim_in || type == isl_dim_out) {
523 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
524 dim->tuple_name[type - isl_dim_in] = NULL;
533 __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
534 enum isl_dim_type dst_type, unsigned dst_pos,
535 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
542 isl_assert(dim->ctx, src_pos + n <= isl_dim_size(dim, src_type),
545 if (dst_type == src_type && dst_pos == src_pos)
548 isl_assert(dim->ctx, dst_type != src_type, goto error);
550 dim = isl_dim_cow(dim);
555 struct isl_name **names;
559 names = isl_calloc_array(dim->ctx, struct isl_name *,
560 dim->nparam + dim->n_in + dim->n_out);
564 size[isl_dim_param] = dim->nparam;
565 size[isl_dim_in] = dim->n_in;
566 size[isl_dim_out] = dim->n_out;
567 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
569 get_names(dim, t, 0, dst_pos, names + off);
571 get_names(dim, src_type, src_pos, n, names+off);
573 get_names(dim, t, dst_pos, size[t] - dst_pos,
575 off += size[t] - dst_pos;
576 } else if (t == src_type) {
577 get_names(dim, t, 0, src_pos, names + off);
579 get_names(dim, t, src_pos + n,
580 size[t] - src_pos - n, names + off);
581 off += size[t] - src_pos - n;
583 get_names(dim, t, 0, size[t], names + off);
589 dim->n_name = dim->nparam + dim->n_in + dim->n_out;
593 case isl_dim_param: dim->nparam += n; break;
594 case isl_dim_in: dim->n_in += n; break;
595 case isl_dim_out: dim->n_out += n; break;
599 case isl_dim_param: dim->nparam -= n; break;
600 case isl_dim_in: dim->n_in -= n; break;
601 case isl_dim_out: dim->n_out -= n; break;
610 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
617 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
619 isl_assert(left->ctx,
620 isl_dim_tuple_match(left, isl_dim_out, right, isl_dim_in),
623 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
627 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
628 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
629 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
631 if (dim && left->tuple_name[0] &&
632 !(dim->tuple_name[0] = isl_name_copy(dim->ctx, left->tuple_name[0])))
634 if (dim && right->tuple_name[1] &&
635 !(dim->tuple_name[1] = isl_name_copy(dim->ctx, right->tuple_name[1])))
648 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
655 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
658 dim = isl_dim_alloc(left->ctx, left->nparam,
659 left->n_in + right->n_in, left->n_out + right->n_out);
663 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
664 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
665 dim = copy_names(dim, isl_dim_in, left->n_in, right, isl_dim_in);
666 dim = copy_names(dim, isl_dim_out, 0, left, isl_dim_out);
667 dim = copy_names(dim, isl_dim_out, left->n_out, right, isl_dim_out);
679 struct isl_dim *isl_dim_map(struct isl_dim *dim)
681 struct isl_name **names = NULL;
685 isl_assert(dim->ctx, dim->n_in == 0, goto error);
686 if (dim->n_out == 0 && !dim->tuple_name[1])
688 dim = isl_dim_cow(dim);
692 names = isl_calloc_array(dim->ctx, struct isl_name *,
693 dim->nparam + dim->n_out + dim->n_out);
696 get_names(dim, isl_dim_param, 0, dim->nparam, names);
697 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
699 dim->n_in = dim->n_out;
703 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
704 dim = copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
706 isl_name_free(dim->ctx, dim->tuple_name[0]);
707 dim->tuple_name[0] = isl_name_copy(dim->ctx, dim->tuple_name[1]);
714 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
715 unsigned first, unsigned n, struct isl_name **names)
719 for (i = 0; i < n ; ++i)
720 dim = set_name(dim, type, first+i, names[i]);
725 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
728 struct isl_name **names = NULL;
729 struct isl_name *name;
733 if (match(dim, isl_dim_in, dim, isl_dim_out))
736 dim = isl_dim_cow(dim);
740 name = dim->tuple_name[0];
741 dim->tuple_name[0] = dim->tuple_name[1];
742 dim->tuple_name[1] = name;
745 names = isl_alloc_array(dim->ctx, struct isl_name *,
746 dim->n_in + dim->n_out);
749 get_names(dim, isl_dim_in, 0, dim->n_in, names);
750 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
754 dim->n_in = dim->n_out;
758 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
759 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
770 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
771 unsigned first, unsigned num)
781 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
782 dim = isl_dim_cow(dim);
786 dim = extend_names(dim);
789 for (i = 0; i < num; ++i)
790 isl_name_free(dim->ctx, get_name(dim, type, first+i));
791 for (i = first+num; i < n(dim, type); ++i)
792 set_name(dim, type, i - num, get_name(dim, type, i));
795 get_names(dim, isl_dim_in, 0, dim->n_in,
796 dim->names + offset(dim, isl_dim_in) - num);
798 get_names(dim, isl_dim_out, 0, dim->n_out,
799 dim->names + offset(dim, isl_dim_out) - num);
806 case isl_dim_param: dim->nparam -= num; break;
807 case isl_dim_in: dim->n_in -= num; break;
808 case isl_dim_out: dim->n_out -= num; break;
810 if (type == isl_dim_in || type == isl_dim_out) {
811 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
812 dim->tuple_name[type - isl_dim_in] = NULL;
820 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
821 unsigned first, unsigned n)
825 return isl_dim_drop(dim, isl_dim_in, first, n);
828 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
829 unsigned first, unsigned n)
833 return isl_dim_drop(dim, isl_dim_out, first, n);
836 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
840 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
841 return isl_dim_reverse(dim);
844 struct isl_dim *isl_dim_range(struct isl_dim *dim)
848 return isl_dim_drop_inputs(dim, 0, dim->n_in);
851 __isl_give isl_dim *isl_dim_as_set_dim(__isl_take isl_dim *dim)
853 dim = isl_dim_cow(dim);
857 dim->n_out += dim->n_in;
859 isl_name_free(dim->ctx, dim->tuple_name[0]);
860 isl_name_free(dim->ctx, dim->tuple_name[1]);
861 dim->tuple_name[0] = NULL;
862 dim->tuple_name[1] = NULL;
867 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
874 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0 &&
877 dim = isl_dim_cow(dim);
880 dim->n_out += dim->nparam + dim->n_in + n_div;
884 for (i = 0; i < dim->n_name; ++i)
885 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
887 isl_name_free(dim->ctx, dim->tuple_name[0]);
888 isl_name_free(dim->ctx, dim->tuple_name[1]);
889 dim->tuple_name[0] = NULL;
890 dim->tuple_name[1] = NULL;
895 unsigned isl_dim_total(struct isl_dim *dim)
897 return dim->nparam + dim->n_in + dim->n_out;
900 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
902 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
903 isl_dim_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
904 isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
907 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
909 return dim1->nparam == dim2->nparam &&
910 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
913 uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
917 struct isl_name *name;
921 hash = isl_hash_init();
923 hash = isl_hash_builtin(hash, dim->nparam);
924 hash = isl_hash_builtin(hash, dim->n_in);
925 hash = isl_hash_builtin(hash, dim->n_out);
927 for (i = 0; i < dim->nparam; ++i) {
928 name = get_name(dim, isl_dim_param, i);
929 hash = isl_hash_builtin(hash, name);
932 name = tuple_name(dim, isl_dim_in);
933 hash = isl_hash_builtin(hash, name);
934 name = tuple_name(dim, isl_dim_out);
935 hash = isl_hash_builtin(hash, name);