1/*
2 * Copyright 2013      Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
9
10#include <isl/space.h>
11
12#include <isl_multi_macro.h>
13
14/* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
15 * to dimensions of "dst_type" at "dst_pos".
16 *
17 * We only support moving input dimensions to parameters and vice versa.
18 */
19__isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
20	enum isl_dim_type dst_type, unsigned dst_pos,
21	enum isl_dim_type src_type, unsigned src_pos, unsigned n)
22{
23	isl_space *space;
24	isl_size size;
25	int i;
26
27	size = FN(MULTI(BASE),size)(multi);
28	if (size < 0)
29		return FN(MULTI(BASE),free)(multi);
30
31	if (n == 0 &&
32	    !isl_space_is_named_or_nested(multi->space, src_type) &&
33	    !isl_space_is_named_or_nested(multi->space, dst_type))
34		return multi;
35
36	if (dst_type == isl_dim_out || src_type == isl_dim_out)
37		isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
38			"cannot move output/set dimension",
39			return FN(MULTI(BASE),free)(multi));
40	if (dst_type == isl_dim_div || src_type == isl_dim_div)
41		isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
42			"cannot move divs",
43			return FN(MULTI(BASE),free)(multi));
44	if (FN(MULTI(BASE),check_range)(multi, src_type, src_pos, n) < 0)
45		return FN(MULTI(BASE),free)(multi);
46	if (dst_type == src_type)
47		isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
48			"moving dims within the same type not supported",
49			return FN(MULTI(BASE),free)(multi));
50
51	space = FN(MULTI(BASE),take_space)(multi);
52	space = isl_space_move_dims(space, dst_type, dst_pos,
53						src_type, src_pos, n);
54	multi = FN(MULTI(BASE),restore_space)(multi, space);
55
56	if (FN(MULTI(BASE),has_explicit_domain)(multi))
57		multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
58				dst_type, dst_pos, src_type, src_pos, n);
59
60	for (i = 0; i < size; ++i) {
61		EL *el;
62
63		el = FN(MULTI(BASE),take_at)(multi, i);
64		el = FN(EL,move_dims)(el, dst_type, dst_pos,
65						src_type, src_pos, n);
66		multi = FN(MULTI(BASE),restore_at)(multi, i, el);
67	}
68
69	return multi;
70}
71