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
;
148 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
151 case isl_dim_param
: return dim
->nparam
;
152 case isl_dim_in
: return dim
->n_in
;
153 case isl_dim_out
: return dim
->n_out
;
157 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
164 static struct isl_dim
*copy_names(struct isl_dim
*dst
,
165 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
166 enum isl_dim_type src_type
)
169 struct isl_name
*name
;
171 for (i
= 0; i
< n(src
, src_type
); ++i
) {
172 name
= get_name(src
, src_type
, i
);
175 dst
= set_name(dst
, dst_type
, offset
+ i
,
176 isl_name_copy(dst
->ctx
, name
));
183 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
186 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
189 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
190 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
191 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
195 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
203 return isl_dim_dup(dim
);
206 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
215 void isl_dim_free(struct isl_dim
*dim
)
225 for (i
= 0; i
< dim
->n_name
; ++i
)
226 isl_name_free(dim
->ctx
, dim
->names
[i
]);
228 isl_ctx_deref(dim
->ctx
);
233 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
234 enum isl_dim_type type
, unsigned pos
,
237 struct isl_name
*name
;
240 name
= isl_name_get(dim
->ctx
, s
);
243 return set_name(dim
, type
, pos
, name
);
249 const char *isl_dim_get_name(struct isl_dim
*dim
,
250 enum isl_dim_type type
, unsigned pos
)
252 struct isl_name
*name
= get_name(dim
, type
, pos
);
253 return name
? name
->name
: NULL
;
256 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
257 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
261 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
264 if (!dim1
->names
&& !dim2
->names
)
267 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
268 if (get_name(dim1
, dim1_type
, i
) !=
269 get_name(dim2
, dim2_type
, i
))
275 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
276 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
278 return match(dim1
, dim1_type
, dim2
, dim2_type
);
281 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
282 unsigned first
, unsigned n
, struct isl_name
**names
)
286 for (i
= 0; i
< n
; ++i
)
287 names
[i
] = get_name(dim
, type
, first
+i
);
290 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
291 unsigned nparam
, unsigned n_in
, unsigned n_out
)
293 struct isl_name
**names
= NULL
;
297 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
300 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
301 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
302 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
304 dim
= isl_dim_cow(dim
);
307 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
308 nparam
+ n_in
+ n_out
);
311 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
312 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
313 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
314 names
+ nparam
+ n_in
);
317 dim
->n_name
= nparam
+ n_in
+ n_out
;
319 dim
->nparam
= nparam
;
330 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
335 return isl_dim_extend(dim
,
336 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
338 return isl_dim_extend(dim
,
339 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
341 return isl_dim_extend(dim
,
342 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
347 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
354 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
356 isl_assert(left
->ctx
, n(left
, isl_dim_out
) == n(right
, isl_dim_in
),
359 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
363 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
364 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
365 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
377 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
384 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
387 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
,
388 left
->n_in
+ right
->n_in
, left
->n_out
+ right
->n_out
);
392 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
393 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
394 dim
= copy_names(dim
, isl_dim_in
, left
->n_in
, right
, isl_dim_in
);
395 dim
= copy_names(dim
, isl_dim_out
, 0, left
, isl_dim_out
);
396 dim
= copy_names(dim
, isl_dim_out
, left
->n_out
, right
, isl_dim_out
);
408 struct isl_dim
*isl_dim_map(struct isl_dim
*dim
)
410 struct isl_name
**names
= NULL
;
414 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
417 dim
= isl_dim_cow(dim
);
421 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
422 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
425 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
426 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
428 dim
->n_in
= dim
->n_out
;
432 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
433 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
441 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
442 unsigned first
, unsigned n
, struct isl_name
**names
)
446 for (i
= 0; i
< n
; ++i
)
447 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
452 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
455 struct isl_name
**names
= NULL
;
459 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
462 dim
= isl_dim_cow(dim
);
467 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
468 dim
->n_in
+ dim
->n_out
);
471 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
472 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
476 dim
->n_in
= dim
->n_out
;
480 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
481 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
492 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
493 unsigned first
, unsigned num
)
503 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
504 dim
= isl_dim_cow(dim
);
508 dim
= extend_names(dim
);
511 for (i
= 0; i
< num
; ++i
)
512 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
513 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
514 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
517 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
518 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
520 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
521 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
528 case isl_dim_param
: dim
->nparam
-= num
; break;
529 case isl_dim_in
: dim
->n_in
-= num
; break;
530 case isl_dim_out
: dim
->n_out
-= num
; break;
538 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
539 unsigned first
, unsigned n
)
541 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
544 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
545 unsigned first
, unsigned n
)
547 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
550 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
554 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
555 return isl_dim_reverse(dim
);
558 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
562 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
565 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
572 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
574 dim
= isl_dim_cow(dim
);
577 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
581 for (i
= 0; i
< dim
->n_name
; ++i
)
582 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
588 unsigned isl_dim_total(struct isl_dim
*dim
)
590 return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
593 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
595 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
596 n(dim1
, isl_dim_in
) == n(dim2
, isl_dim_in
) &&
597 n(dim1
, isl_dim_out
) == n(dim2
, isl_dim_out
);
600 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
602 return dim1
->nparam
== dim2
->nparam
&&
603 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;