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
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
);
36 struct isl_dim
*isl_dim_set_alloc(struct isl_ctx
*ctx
,
37 unsigned nparam
, unsigned dim
)
39 return isl_dim_alloc(ctx
, nparam
, 0, dim
);
42 static unsigned global_pos(struct isl_dim
*dim
,
43 enum isl_dim_type type
, unsigned pos
)
45 struct isl_ctx
*ctx
= dim
->ctx
;
49 isl_assert(ctx
, pos
< dim
->nparam
, return isl_dim_total(dim
));
52 isl_assert(ctx
, pos
< dim
->n_in
, return isl_dim_total(dim
));
53 return pos
+ dim
->nparam
;
55 isl_assert(ctx
, pos
< dim
->n_out
, return isl_dim_total(dim
));
56 return pos
+ dim
->nparam
+ dim
->n_in
;
58 isl_assert(ctx
, 0, return isl_dim_total(dim
));
60 return isl_dim_total(dim
);
63 /* Extend length of names array to the total number of dimensions.
65 static __isl_give isl_dim
*extend_names(__isl_take isl_dim
*dim
)
67 struct isl_name
**names
;
70 if (isl_dim_total(dim
) <= dim
->n_name
)
74 dim
->names
= isl_calloc_array(dim
->ctx
,
75 struct isl_name
*, isl_dim_total(dim
));
79 names
= isl_realloc_array(dim
->ctx
, dim
->names
,
80 struct isl_name
*, isl_dim_total(dim
));
84 for (i
= dim
->n_name
; i
< isl_dim_total(dim
); ++i
)
88 dim
->n_name
= isl_dim_total(dim
);
96 static struct isl_dim
*set_name(struct isl_dim
*dim
,
97 enum isl_dim_type type
, unsigned pos
,
98 struct isl_name
*name
)
100 struct isl_ctx
*ctx
= dim
->ctx
;
101 dim
= isl_dim_cow(dim
);
106 pos
= global_pos(dim
, type
, pos
);
107 isl_assert(ctx
, pos
!= isl_dim_total(dim
), goto error
);
109 if (pos
>= dim
->n_name
) {
112 dim
= extend_names(dim
);
117 dim
->names
[pos
] = name
;
121 isl_name_free(ctx
, name
);
126 static struct isl_name
*get_name(struct isl_dim
*dim
,
127 enum isl_dim_type type
, unsigned pos
)
132 pos
= global_pos(dim
, type
, pos
);
133 if (pos
== isl_dim_total(dim
))
135 if (pos
>= dim
->n_name
)
137 return dim
->names
[pos
];
140 static unsigned offset(struct isl_dim
*dim
, enum isl_dim_type type
)
143 case isl_dim_param
: return 0;
144 case isl_dim_in
: return dim
->nparam
;
145 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
150 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
153 case isl_dim_param
: return dim
->nparam
;
154 case isl_dim_in
: return dim
->n_in
;
155 case isl_dim_out
: return dim
->n_out
;
160 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
167 unsigned isl_dim_offset(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
171 return offset(dim
, type
);
174 static struct isl_dim
*copy_names(struct isl_dim
*dst
,
175 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
176 enum isl_dim_type src_type
)
179 struct isl_name
*name
;
184 for (i
= 0; i
< n(src
, src_type
); ++i
) {
185 name
= get_name(src
, src_type
, i
);
188 dst
= set_name(dst
, dst_type
, offset
+ i
,
189 isl_name_copy(dst
->ctx
, name
));
196 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
201 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
204 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
205 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
206 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
210 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
218 return isl_dim_dup(dim
);
221 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
230 void isl_dim_free(struct isl_dim
*dim
)
240 for (i
= 0; i
< dim
->n_name
; ++i
)
241 isl_name_free(dim
->ctx
, dim
->names
[i
]);
243 isl_ctx_deref(dim
->ctx
);
248 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
249 enum isl_dim_type type
, unsigned pos
,
252 struct isl_name
*name
;
258 dummy
= strtol(s
, &p
, 0);
260 isl_die(dim
->ctx
, isl_error_invalid
, "name looks like a number",
262 name
= isl_name_get(dim
->ctx
, s
);
265 return set_name(dim
, type
, pos
, name
);
271 const char *isl_dim_get_name(struct isl_dim
*dim
,
272 enum isl_dim_type type
, unsigned pos
)
274 struct isl_name
*name
= get_name(dim
, type
, pos
);
275 return name
? name
->name
: NULL
;
278 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
279 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
283 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
286 if (!dim1
->names
&& !dim2
->names
)
289 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
290 if (get_name(dim1
, dim1_type
, i
) !=
291 get_name(dim2
, dim2_type
, i
))
297 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
298 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
300 return match(dim1
, dim1_type
, dim2
, dim2_type
);
303 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
304 unsigned first
, unsigned n
, struct isl_name
**names
)
308 for (i
= 0; i
< n
; ++i
)
309 names
[i
] = get_name(dim
, type
, first
+i
);
312 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
313 unsigned nparam
, unsigned n_in
, unsigned n_out
)
315 struct isl_name
**names
= NULL
;
319 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
322 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
323 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
324 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
326 dim
= isl_dim_cow(dim
);
329 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
330 nparam
+ n_in
+ n_out
);
333 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
334 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
335 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
336 names
+ nparam
+ n_in
);
339 dim
->n_name
= nparam
+ n_in
+ n_out
;
341 dim
->nparam
= nparam
;
352 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
357 return isl_dim_extend(dim
,
358 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
360 return isl_dim_extend(dim
,
361 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
363 return isl_dim_extend(dim
,
364 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
369 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
370 enum isl_dim_type type
, unsigned pos
, unsigned n
)
372 struct isl_name
**names
= NULL
;
379 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
381 dim
= isl_dim_cow(dim
);
389 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
390 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
394 size
[isl_dim_param
] = dim
->nparam
;
395 size
[isl_dim_in
] = dim
->n_in
;
396 size
[isl_dim_out
] = dim
->n_out
;
397 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
399 get_names(dim
, t
, 0, size
[t
], names
+ off
);
402 get_names(dim
, t
, 0, pos
, names
+ off
);
404 get_names(dim
, t
, pos
, size
[t
]-pos
, names
+off
);
405 off
+= size
[t
] - pos
;
410 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
413 case isl_dim_param
: dim
->nparam
+= n
; break;
414 case isl_dim_in
: dim
->n_in
+= n
; break;
415 case isl_dim_out
: dim
->n_out
+= n
; break;
424 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
425 enum isl_dim_type dst_type
, unsigned dst_pos
,
426 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
433 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
436 if (dst_type
== src_type
&& dst_pos
== src_pos
)
439 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
441 dim
= isl_dim_cow(dim
);
446 struct isl_name
**names
;
450 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
451 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
455 size
[isl_dim_param
] = dim
->nparam
;
456 size
[isl_dim_in
] = dim
->n_in
;
457 size
[isl_dim_out
] = dim
->n_out
;
458 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
460 get_names(dim
, t
, 0, dst_pos
, names
+ off
);
462 get_names(dim
, src_type
, src_pos
, n
, names
+off
);
464 get_names(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
466 off
+= size
[t
] - dst_pos
;
467 } else if (t
== src_type
) {
468 get_names(dim
, t
, 0, src_pos
, names
+ off
);
470 get_names(dim
, t
, src_pos
+ n
,
471 size
[t
] - src_pos
- n
, names
+ off
);
472 off
+= size
[t
] - src_pos
- n
;
474 get_names(dim
, t
, 0, size
[t
], names
+ off
);
480 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
484 case isl_dim_param
: dim
->nparam
+= n
; break;
485 case isl_dim_in
: dim
->n_in
+= n
; break;
486 case isl_dim_out
: dim
->n_out
+= n
; break;
490 case isl_dim_param
: dim
->nparam
-= n
; break;
491 case isl_dim_in
: dim
->n_in
-= n
; break;
492 case isl_dim_out
: dim
->n_out
-= n
; break;
501 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
508 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
510 isl_assert(left
->ctx
, n(left
, isl_dim_out
) == n(right
, isl_dim_in
),
513 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
517 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
518 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
519 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
531 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
538 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
541 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
,
542 left
->n_in
+ right
->n_in
, left
->n_out
+ right
->n_out
);
546 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
547 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
548 dim
= copy_names(dim
, isl_dim_in
, left
->n_in
, right
, isl_dim_in
);
549 dim
= copy_names(dim
, isl_dim_out
, 0, left
, isl_dim_out
);
550 dim
= copy_names(dim
, isl_dim_out
, left
->n_out
, right
, isl_dim_out
);
562 struct isl_dim
*isl_dim_map(struct isl_dim
*dim
)
564 struct isl_name
**names
= NULL
;
568 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
571 dim
= isl_dim_cow(dim
);
575 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
576 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
579 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
580 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
582 dim
->n_in
= dim
->n_out
;
586 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
587 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
595 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
596 unsigned first
, unsigned n
, struct isl_name
**names
)
600 for (i
= 0; i
< n
; ++i
)
601 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
606 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
609 struct isl_name
**names
= NULL
;
613 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
616 dim
= isl_dim_cow(dim
);
621 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
622 dim
->n_in
+ dim
->n_out
);
625 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
626 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
630 dim
->n_in
= dim
->n_out
;
634 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
635 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
646 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
647 unsigned first
, unsigned num
)
657 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
658 dim
= isl_dim_cow(dim
);
662 dim
= extend_names(dim
);
665 for (i
= 0; i
< num
; ++i
)
666 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
667 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
668 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
671 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
672 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
674 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
675 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
682 case isl_dim_param
: dim
->nparam
-= num
; break;
683 case isl_dim_in
: dim
->n_in
-= num
; break;
684 case isl_dim_out
: dim
->n_out
-= num
; break;
692 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
693 unsigned first
, unsigned n
)
695 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
698 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
699 unsigned first
, unsigned n
)
701 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
704 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
708 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
709 return isl_dim_reverse(dim
);
712 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
716 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
719 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
726 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
728 dim
= isl_dim_cow(dim
);
731 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
735 for (i
= 0; i
< dim
->n_name
; ++i
)
736 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
742 unsigned isl_dim_total(struct isl_dim
*dim
)
744 return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
747 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
749 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
750 n(dim1
, isl_dim_in
) == n(dim2
, isl_dim_in
) &&
751 n(dim1
, isl_dim_out
) == n(dim2
, isl_dim_out
);
754 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
756 return dim1
->nparam
== dim2
->nparam
&&
757 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;