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
13 struct isl_dim
*isl_dim_alloc(struct isl_ctx
*ctx
,
14 unsigned nparam
, unsigned n_in
, unsigned n_out
)
18 dim
= isl_alloc_type(ctx
, struct isl_dim
);
35 struct isl_dim
*isl_dim_set_alloc(struct isl_ctx
*ctx
,
36 unsigned nparam
, unsigned dim
)
38 return isl_dim_alloc(ctx
, nparam
, 0, dim
);
41 static unsigned global_pos(struct isl_dim
*dim
,
42 enum isl_dim_type type
, unsigned pos
)
44 struct isl_ctx
*ctx
= dim
->ctx
;
48 isl_assert(ctx
, pos
< dim
->nparam
, return isl_dim_total(dim
));
51 isl_assert(ctx
, pos
< dim
->n_in
, return isl_dim_total(dim
));
52 return pos
+ dim
->nparam
;
54 isl_assert(ctx
, pos
< dim
->n_out
, return isl_dim_total(dim
));
55 return pos
+ dim
->nparam
+ dim
->n_in
;
57 isl_assert(ctx
, 0, return isl_dim_total(dim
));
59 return isl_dim_total(dim
);
62 /* Extend length of names array to the total number of dimensions.
64 static __isl_give isl_dim
*extend_names(__isl_take isl_dim
*dim
)
66 struct isl_name
**names
;
69 if (isl_dim_total(dim
) <= dim
->n_name
)
73 dim
->names
= isl_calloc_array(dim
->ctx
,
74 struct isl_name
*, isl_dim_total(dim
));
78 names
= isl_realloc_array(dim
->ctx
, dim
->names
,
79 struct isl_name
*, isl_dim_total(dim
));
83 for (i
= dim
->n_name
; i
< isl_dim_total(dim
); ++i
)
87 dim
->n_name
= isl_dim_total(dim
);
95 static struct isl_dim
*set_name(struct isl_dim
*dim
,
96 enum isl_dim_type type
, unsigned pos
,
97 struct isl_name
*name
)
99 struct isl_ctx
*ctx
= dim
->ctx
;
100 dim
= isl_dim_cow(dim
);
105 pos
= global_pos(dim
, type
, pos
);
106 isl_assert(ctx
, pos
!= isl_dim_total(dim
), goto error
);
108 if (pos
>= dim
->n_name
) {
111 dim
= extend_names(dim
);
116 dim
->names
[pos
] = name
;
120 isl_name_free(ctx
, name
);
125 static struct isl_name
*get_name(struct isl_dim
*dim
,
126 enum isl_dim_type type
, unsigned pos
)
131 pos
= global_pos(dim
, type
, pos
);
132 if (pos
== isl_dim_total(dim
))
134 if (pos
>= dim
->n_name
)
136 return dim
->names
[pos
];
139 static unsigned offset(struct isl_dim
*dim
, enum isl_dim_type type
)
142 case isl_dim_param
: return 0;
143 case isl_dim_in
: return dim
->nparam
;
144 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
149 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
152 case isl_dim_param
: return dim
->nparam
;
153 case isl_dim_in
: return dim
->n_in
;
154 case isl_dim_out
: return dim
->n_out
;
159 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
166 unsigned isl_dim_offset(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
170 return offset(dim
, type
);
173 static struct isl_dim
*copy_names(struct isl_dim
*dst
,
174 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
175 enum isl_dim_type src_type
)
178 struct isl_name
*name
;
183 for (i
= 0; i
< n(src
, src_type
); ++i
) {
184 name
= get_name(src
, src_type
, i
);
187 dst
= set_name(dst
, dst_type
, offset
+ i
,
188 isl_name_copy(dst
->ctx
, name
));
195 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
200 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
203 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
204 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
205 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
209 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
217 return isl_dim_dup(dim
);
220 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
229 void isl_dim_free(struct isl_dim
*dim
)
239 for (i
= 0; i
< dim
->n_name
; ++i
)
240 isl_name_free(dim
->ctx
, dim
->names
[i
]);
242 isl_ctx_deref(dim
->ctx
);
247 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
248 enum isl_dim_type type
, unsigned pos
,
251 struct isl_name
*name
;
254 name
= isl_name_get(dim
->ctx
, s
);
257 return set_name(dim
, type
, pos
, name
);
263 const char *isl_dim_get_name(struct isl_dim
*dim
,
264 enum isl_dim_type type
, unsigned pos
)
266 struct isl_name
*name
= get_name(dim
, type
, pos
);
267 return name
? name
->name
: NULL
;
270 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
271 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
275 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
278 if (!dim1
->names
&& !dim2
->names
)
281 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
282 if (get_name(dim1
, dim1_type
, i
) !=
283 get_name(dim2
, dim2_type
, i
))
289 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
290 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
292 return match(dim1
, dim1_type
, dim2
, dim2_type
);
295 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
296 unsigned first
, unsigned n
, struct isl_name
**names
)
300 for (i
= 0; i
< n
; ++i
)
301 names
[i
] = get_name(dim
, type
, first
+i
);
304 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
305 unsigned nparam
, unsigned n_in
, unsigned n_out
)
307 struct isl_name
**names
= NULL
;
311 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
314 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
315 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
316 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
318 dim
= isl_dim_cow(dim
);
321 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
322 nparam
+ n_in
+ n_out
);
325 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
326 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
327 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
328 names
+ nparam
+ n_in
);
331 dim
->n_name
= nparam
+ n_in
+ n_out
;
333 dim
->nparam
= nparam
;
344 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
349 return isl_dim_extend(dim
,
350 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
352 return isl_dim_extend(dim
,
353 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
355 return isl_dim_extend(dim
,
356 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
361 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
362 enum isl_dim_type type
, unsigned pos
, unsigned n
)
364 struct isl_name
**names
= NULL
;
371 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
373 dim
= isl_dim_cow(dim
);
381 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
382 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
386 size
[isl_dim_param
] = dim
->nparam
;
387 size
[isl_dim_in
] = dim
->n_in
;
388 size
[isl_dim_out
] = dim
->n_out
;
389 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
391 get_names(dim
, t
, 0, size
[t
], names
+ off
);
394 get_names(dim
, t
, 0, pos
, names
+ off
);
396 get_names(dim
, t
, pos
, size
[t
]-pos
, names
+off
);
397 off
+= size
[t
] - pos
;
402 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
405 case isl_dim_param
: dim
->nparam
+= n
; break;
406 case isl_dim_in
: dim
->n_in
+= n
; break;
407 case isl_dim_out
: dim
->n_out
+= n
; break;
416 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
417 enum isl_dim_type dst_type
, unsigned dst_pos
,
418 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
425 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
428 if (dst_type
== src_type
&& dst_pos
== src_pos
)
431 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
433 dim
= isl_dim_cow(dim
);
438 struct isl_name
**names
;
442 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
443 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
447 size
[isl_dim_param
] = dim
->nparam
;
448 size
[isl_dim_in
] = dim
->n_in
;
449 size
[isl_dim_out
] = dim
->n_out
;
450 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
452 get_names(dim
, t
, 0, dst_pos
, names
+ off
);
454 get_names(dim
, src_type
, src_pos
, n
, names
+off
);
456 get_names(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
458 off
+= size
[t
] - dst_pos
;
459 } else if (t
== src_type
) {
460 get_names(dim
, t
, 0, src_pos
, names
+ off
);
462 get_names(dim
, t
, src_pos
+ n
,
463 size
[t
] - src_pos
- n
, names
+ off
);
464 off
+= size
[t
] - src_pos
- n
;
466 get_names(dim
, t
, 0, size
[t
], names
+ off
);
472 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
476 case isl_dim_param
: dim
->nparam
+= n
; break;
477 case isl_dim_in
: dim
->n_in
+= n
; break;
478 case isl_dim_out
: dim
->n_out
+= n
; break;
482 case isl_dim_param
: dim
->nparam
-= n
; break;
483 case isl_dim_in
: dim
->n_in
-= n
; break;
484 case isl_dim_out
: dim
->n_out
-= n
; break;
493 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
500 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
502 isl_assert(left
->ctx
, n(left
, isl_dim_out
) == n(right
, isl_dim_in
),
505 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
509 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
510 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
511 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
523 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
530 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
533 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
,
534 left
->n_in
+ right
->n_in
, left
->n_out
+ right
->n_out
);
538 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
539 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
540 dim
= copy_names(dim
, isl_dim_in
, left
->n_in
, right
, isl_dim_in
);
541 dim
= copy_names(dim
, isl_dim_out
, 0, left
, isl_dim_out
);
542 dim
= copy_names(dim
, isl_dim_out
, left
->n_out
, right
, isl_dim_out
);
554 struct isl_dim
*isl_dim_map(struct isl_dim
*dim
)
556 struct isl_name
**names
= NULL
;
560 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
563 dim
= isl_dim_cow(dim
);
567 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
568 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
571 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
572 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
574 dim
->n_in
= dim
->n_out
;
578 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
579 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
587 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
588 unsigned first
, unsigned n
, struct isl_name
**names
)
592 for (i
= 0; i
< n
; ++i
)
593 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
598 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
601 struct isl_name
**names
= NULL
;
605 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
608 dim
= isl_dim_cow(dim
);
613 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
614 dim
->n_in
+ dim
->n_out
);
617 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
618 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
622 dim
->n_in
= dim
->n_out
;
626 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
627 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
638 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
639 unsigned first
, unsigned num
)
649 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
650 dim
= isl_dim_cow(dim
);
654 dim
= extend_names(dim
);
657 for (i
= 0; i
< num
; ++i
)
658 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
659 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
660 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
663 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
664 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
666 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
667 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
674 case isl_dim_param
: dim
->nparam
-= num
; break;
675 case isl_dim_in
: dim
->n_in
-= num
; break;
676 case isl_dim_out
: dim
->n_out
-= num
; break;
684 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
685 unsigned first
, unsigned n
)
687 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
690 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
691 unsigned first
, unsigned n
)
693 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
696 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
700 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
701 return isl_dim_reverse(dim
);
704 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
708 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
711 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
718 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
720 dim
= isl_dim_cow(dim
);
723 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
727 for (i
= 0; i
< dim
->n_name
; ++i
)
728 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
734 unsigned isl_dim_total(struct isl_dim
*dim
)
736 return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
739 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
741 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
742 n(dim1
, isl_dim_in
) == n(dim2
, isl_dim_in
) &&
743 n(dim1
, isl_dim_out
) == n(dim2
, isl_dim_out
);
746 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
748 return dim1
->nparam
== dim2
->nparam
&&
749 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;