2 * Copyright 2011 Sven Verdoolaege
4 * Use of this software is governed by the MIT license
7 #define xCAT(A,B) A ## B
8 #define CAT(A,B) xCAT(A,B)
10 #define EL CAT(isl_,BASE)
11 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 #define xMULTI(BASE) isl_multi_ ## BASE
14 #define MULTI(BASE) xMULTI(BASE)
15 #define MULTI_NAME(BASE) "isl_multi_" #BASE
16 #define xLIST(EL) EL ## _list
17 #define LIST(EL) xLIST(EL)
19 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
21 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
24 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
26 return multi
? isl_space_copy(multi
->space
) : NULL
;
29 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
30 __isl_keep
MULTI(BASE
) *multi
)
32 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
35 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
44 ctx
= isl_space_get_ctx(space
);
45 n
= isl_space_dim(space
, isl_dim_out
);
46 multi
= isl_calloc(ctx
, MULTI(BASE
),
47 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
56 isl_space_free(space
);
60 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
68 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
72 for (i
= 0; i
< multi
->n
; ++i
)
73 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
74 FN(EL
,copy
)(multi
->p
[i
]));
79 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
88 return FN(MULTI(BASE
),dup
)(multi
);
91 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
100 void *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
107 if (--multi
->ref
> 0)
110 isl_space_free(multi
->space
);
111 for (i
= 0; i
< multi
->n
; ++i
)
112 FN(EL
,free
)(multi
->p
[i
]);
118 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
119 enum isl_dim_type type
)
121 return multi
? isl_space_dim(multi
->space
, type
) : 0;
124 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
125 __isl_take
MULTI(BASE
) *multi
,
126 enum isl_dim_type type
, unsigned pos
, const char *s
)
130 multi
= FN(MULTI(BASE
),cow
)(multi
);
134 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
136 return FN(MULTI(BASE
),free
)(multi
);
138 if (type
== isl_dim_out
)
140 for (i
= 0; i
< multi
->n
; ++i
) {
141 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
143 return FN(MULTI(BASE
),free
)(multi
);
149 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
150 enum isl_dim_type type
)
152 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
155 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
162 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
163 if (pos
< 0 || pos
>= multi
->n
)
164 isl_die(ctx
, isl_error_invalid
,
165 "index out of bounds", return NULL
);
166 return FN(EL
,copy
)(multi
->p
[pos
]);
169 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
170 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
172 isl_space
*multi_space
= NULL
;
173 isl_space
*el_space
= NULL
;
175 multi
= FN(MULTI(BASE
),cow
)(multi
);
179 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
180 el_space
= FN(EL
,get_space
)(el
);
182 if (!isl_space_match(multi_space
, isl_dim_param
,
183 el_space
, isl_dim_param
))
184 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
185 "parameters don't match", goto error
);
186 if (!isl_space_tuple_match(multi_space
, isl_dim_in
,
187 el_space
, isl_dim_in
))
188 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
189 "domains don't match", goto error
);
191 if (pos
< 0 || pos
>= multi
->n
)
192 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
193 "index out of bounds", goto error
);
195 FN(EL
,free
)(multi
->p
[pos
]);
198 isl_space_free(multi_space
);
199 isl_space_free(el_space
);
203 FN(MULTI(BASE
),free
)(multi
);
205 isl_space_free(multi_space
);
206 isl_space_free(el_space
);
210 /* Reset the space of "multi". This function is called from isl_pw_templ.c
211 * and doesn't know if the space of an element object is represented
212 * directly or through its domain. It therefore passes along both,
213 * which we pass along to the element function since we don't how
214 * that is represented either.
216 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
217 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
218 __isl_take isl_space
*domain
)
222 multi
= FN(MULTI(BASE
),cow
)(multi
);
223 if (!multi
|| !space
|| !domain
)
226 for (i
= 0; i
< multi
->n
; ++i
) {
227 multi
->p
[i
] = FN(EL
,reset_space_and_domain
)(multi
->p
[i
],
228 isl_space_copy(space
), isl_space_copy(domain
));
232 isl_space_free(domain
);
233 isl_space_free(multi
->space
);
234 multi
->space
= space
;
238 isl_space_free(domain
);
239 isl_space_free(space
);
240 FN(MULTI(BASE
),free
)(multi
);
244 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
245 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
249 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
250 isl_space_copy(multi
->space
));
251 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
254 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
255 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
259 domain
= isl_space_domain(isl_space_copy(space
));
260 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
263 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
264 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
265 __isl_take isl_id
*id
)
269 multi
= FN(MULTI(BASE
),cow
)(multi
);
271 return isl_id_free(id
);
273 space
= FN(MULTI(BASE
),get_space
)(multi
);
274 space
= isl_space_set_tuple_id(space
, type
, id
);
276 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
279 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
280 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
284 multi
= FN(MULTI(BASE
),cow
)(multi
);
288 for (i
= 0; i
< multi
->n
; ++i
) {
289 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
290 isl_reordering_copy(exp
));
295 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
296 isl_space_copy(exp
->dim
));
298 isl_reordering_free(exp
);
301 isl_reordering_free(exp
);
302 FN(MULTI(BASE
),free
)(multi
);
306 /* Align the parameters of "multi" to those of "model".
308 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
309 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
313 if (!multi
|| !model
)
316 ctx
= isl_space_get_ctx(model
);
317 if (!isl_space_has_named_params(model
))
318 isl_die(ctx
, isl_error_invalid
,
319 "model has unnamed parameters", goto error
);
320 if (!isl_space_has_named_params(multi
->space
))
321 isl_die(ctx
, isl_error_invalid
,
322 "input has unnamed parameters", goto error
);
323 if (!isl_space_match(multi
->space
, isl_dim_param
,
324 model
, isl_dim_param
)) {
327 model
= isl_space_params(model
);
328 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
329 exp
= isl_reordering_extend_space(exp
,
330 FN(MULTI(BASE
),get_domain_space
)(multi
));
331 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
334 isl_space_free(model
);
337 isl_space_free(model
);
338 FN(MULTI(BASE
),free
)(multi
);
342 static __isl_give
MULTI(BASE
) *align_params_multi_set_and(
343 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
344 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
345 __isl_take isl_set
*set
))
351 if (isl_space_match(multi
->space
, isl_dim_param
,
352 set
->dim
, isl_dim_param
))
353 return fn(multi
, set
);
354 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
355 if (!isl_space_has_named_params(multi
->space
) ||
356 !isl_space_has_named_params(set
->dim
))
357 isl_die(ctx
, isl_error_invalid
,
358 "unaligned unnamed parameters", goto error
);
359 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
360 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
361 return fn(multi
, set
);
363 FN(MULTI(BASE
),free
)(multi
);
368 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
369 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
373 if (!multi
|| !context
)
376 for (i
= 0; i
< multi
->n
; ++i
) {
377 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
382 isl_set_free(context
);
385 isl_set_free(context
);
386 FN(MULTI(BASE
),free
)(multi
);
390 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
391 __isl_take isl_set
*context
)
393 return align_params_multi_set_and(multi
, context
,
394 &FN(MULTI(BASE
),gist_aligned
));
397 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
398 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
400 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
401 isl_set
*dom_context
= isl_set_universe(space
);
402 dom_context
= isl_set_intersect_params(dom_context
, context
);
403 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
406 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
407 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
417 ctx
= isl_space_get_ctx(space
);
418 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
419 if (n
!= isl_space_dim(space
, isl_dim_out
))
420 isl_die(ctx
, isl_error_invalid
,
421 "invalid number of elements in list", goto error
);
423 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
424 for (i
= 0; i
< n
; ++i
) {
425 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
426 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
429 isl_space_free(space
);
430 FN(LIST(EL
),free
)(list
);
433 isl_space_free(space
);
434 FN(LIST(EL
),free
)(list
);
438 /* Create a multi expression in the given space that maps each
439 * input dimension to the corresponding output dimension.
441 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
450 if (isl_space_is_set(space
))
451 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
452 "expecting map space", goto error
);
454 n
= isl_space_dim(space
, isl_dim_out
);
455 if (n
!= isl_space_dim(space
, isl_dim_in
))
456 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
457 "number of input and output dimensions needs to be "
458 "the same", goto error
);
460 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
463 isl_space_free(space
);
467 space
= isl_space_domain(space
);
468 ls
= isl_local_space_from_space(space
);
470 for (i
= 0; i
< n
; ++i
) {
472 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
474 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
477 isl_local_space_free(ls
);
481 isl_space_free(space
);
485 /* Construct a multi expression in the given space with value zero in
486 * each of the output dimensions.
488 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
496 n
= isl_space_dim(space
, isl_dim_out
);
497 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
500 isl_space_free(space
);
506 space
= isl_space_domain(space
);
507 ls
= isl_local_space_from_space(space
);
508 el
= FN(EL
,zero_on_domain
)(ls
);
510 for (i
= 0; i
< n
; ++i
)
511 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,