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 static struct isl_dim
*set_name(struct isl_dim
*dim
,
63 enum isl_dim_type type
, unsigned pos
,
64 struct isl_name
*name
)
66 struct isl_ctx
*ctx
= dim
->ctx
;
67 dim
= isl_dim_cow(dim
);
72 pos
= global_pos(dim
, type
, pos
);
73 isl_assert(ctx
, pos
!= isl_dim_total(dim
), goto error
);
75 if (pos
>= dim
->n_name
) {
79 dim
->names
= isl_calloc_array(dim
->ctx
,
80 struct isl_name
*, isl_dim_total(dim
));
85 dim
->names
= isl_realloc_array(dim
->ctx
, dim
->names
,
86 struct isl_name
*, isl_dim_total(dim
));
89 for (i
= dim
->n_name
; i
< isl_dim_total(dim
); ++i
)
92 dim
->n_name
= isl_dim_total(dim
);
95 dim
->names
[pos
] = name
;
99 isl_name_free(ctx
, name
);
104 static struct isl_name
*get_name(struct isl_dim
*dim
,
105 enum isl_dim_type type
, unsigned pos
)
110 pos
= global_pos(dim
, type
, pos
);
111 if (pos
== isl_dim_total(dim
))
113 if (pos
>= dim
->n_name
)
115 return dim
->names
[pos
];
118 static unsigned offset(struct isl_dim
*dim
, enum isl_dim_type type
)
121 case isl_dim_param
: return 0;
122 case isl_dim_in
: return dim
->nparam
;
123 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
127 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
130 case isl_dim_param
: return dim
->nparam
;
131 case isl_dim_in
: return dim
->n_in
;
132 case isl_dim_out
: return dim
->n_out
;
136 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
143 static struct isl_dim
*copy_names(struct isl_dim
*dst
,
144 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
145 enum isl_dim_type src_type
)
148 struct isl_name
*name
;
150 for (i
= 0; i
< n(src
, src_type
); ++i
) {
151 name
= get_name(src
, src_type
, i
);
154 dst
= set_name(dst
, dst_type
, offset
+ i
,
155 isl_name_copy(dst
->ctx
, name
));
162 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
165 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
168 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
169 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
170 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
174 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
182 return isl_dim_dup(dim
);
185 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
194 void isl_dim_free(struct isl_dim
*dim
)
204 for (i
= 0; i
< dim
->n_name
; ++i
)
205 isl_name_free(dim
->ctx
, dim
->names
[i
]);
207 isl_ctx_deref(dim
->ctx
);
212 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
213 enum isl_dim_type type
, unsigned pos
,
216 struct isl_name
*name
;
219 name
= isl_name_get(dim
->ctx
, s
);
222 return set_name(dim
, type
, pos
, name
);
228 const char *isl_dim_get_name(struct isl_dim
*dim
,
229 enum isl_dim_type type
, unsigned pos
)
231 struct isl_name
*name
= get_name(dim
, type
, pos
);
232 return name
? name
->name
: NULL
;
235 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
236 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
240 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
243 if (!dim1
->names
&& !dim2
->names
)
246 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
247 if (get_name(dim1
, dim1_type
, i
) !=
248 get_name(dim2
, dim2_type
, i
))
254 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
255 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
257 return match(dim1
, dim1_type
, dim2
, dim2_type
);
260 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
261 unsigned first
, unsigned n
, struct isl_name
**names
)
265 for (i
= 0; i
< n
; ++i
)
266 names
[i
] = get_name(dim
, type
, first
+i
);
269 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
270 unsigned nparam
, unsigned n_in
, unsigned n_out
)
272 struct isl_name
**names
= NULL
;
276 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
279 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
280 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
281 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
283 dim
= isl_dim_cow(dim
);
286 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
287 nparam
+ n_in
+ n_out
);
290 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
291 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
292 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
293 names
+ nparam
+ n_in
);
296 dim
->n_name
= nparam
+ n_in
+ n_out
;
298 dim
->nparam
= nparam
;
309 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
314 return isl_dim_extend(dim
,
315 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
317 return isl_dim_extend(dim
,
318 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
320 return isl_dim_extend(dim
,
321 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
326 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
333 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
335 isl_assert(left
->ctx
, match(left
, isl_dim_out
, right
, isl_dim_in
),
338 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
342 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
343 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
344 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
356 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
363 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
366 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
,
367 left
->n_in
+ right
->n_in
, left
->n_out
+ right
->n_out
);
371 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
372 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
373 dim
= copy_names(dim
, isl_dim_in
, left
->n_in
, right
, isl_dim_in
);
374 dim
= copy_names(dim
, isl_dim_out
, 0, left
, isl_dim_out
);
375 dim
= copy_names(dim
, isl_dim_out
, left
->n_out
, right
, isl_dim_out
);
387 struct isl_dim
*isl_dim_map(struct isl_dim
*dim
)
389 struct isl_name
**names
= NULL
;
393 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
396 dim
= isl_dim_cow(dim
);
400 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
401 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
404 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
405 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
407 dim
->n_in
= dim
->n_out
;
409 copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
412 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
420 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
421 unsigned first
, unsigned n
, struct isl_name
**names
)
425 for (i
= 0; i
< n
; ++i
)
426 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
431 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
434 struct isl_name
**names
= NULL
;
438 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
441 dim
= isl_dim_cow(dim
);
446 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
447 dim
->n_in
+ dim
->n_out
);
450 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
451 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
455 dim
->n_in
= dim
->n_out
;
459 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
460 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
471 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
472 unsigned first
, unsigned num
)
482 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
483 dim
= isl_dim_cow(dim
);
487 for (i
= 0; i
< num
; ++i
)
488 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
489 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
490 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
493 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
494 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
496 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
497 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
503 case isl_dim_param
: dim
->nparam
-= num
; break;
504 case isl_dim_in
: dim
->n_in
-= num
; break;
505 case isl_dim_out
: dim
->n_out
-= num
; break;
513 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
514 unsigned first
, unsigned n
)
516 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
519 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
520 unsigned first
, unsigned n
)
522 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
525 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
529 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
530 return isl_dim_reverse(dim
);
533 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
537 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
540 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
547 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
549 dim
= isl_dim_cow(dim
);
552 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
556 for (i
= 0; i
< dim
->n_name
; ++i
)
557 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
563 unsigned isl_dim_total(struct isl_dim
*dim
)
565 return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
568 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
570 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
571 match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
572 match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
575 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
577 return dim1
->nparam
== dim2
->nparam
&&
578 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;