2 * Copyright 2011 Sven Verdoolaege
4 * Use of this software is governed by the GNU LGPLv2.1 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 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
125 enum isl_dim_type type)
127 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
130 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
137 ctx = FN(MULTI(BASE),get_ctx)(multi);
138 if (pos < 0 || pos >= multi->n)
139 isl_die(ctx, isl_error_invalid,
140 "index out of bounds", return NULL);
141 return FN(EL,copy)(multi->p[pos]);
144 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
145 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
150 FN(EL,free)(multi->p[pos]);
155 FN(MULTI(BASE),free)(multi);
160 /* Reset the space of "multi". This function is called from isl_pw_templ.c
161 * and doesn't know if the space of an element object is represented
162 * directly or through its domain. It therefore passes along both,
163 * which we pass along to the element function since we don't how
164 * that is represented either.
166 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
167 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
168 __isl_take isl_space *domain)
172 multi = FN(MULTI(BASE),cow)(multi);
173 if (!multi || !space || !domain)
176 for (i = 0; i < multi->n; ++i) {
177 multi->p[i] = FN(EL,reset_space_and_domain)(multi->p[i],
178 isl_space_copy(space), isl_space_copy(domain));
182 isl_space_free(domain);
183 isl_space_free(multi->space);
184 multi->space = space;
188 isl_space_free(domain);
189 isl_space_free(space);
190 FN(MULTI(BASE),free)(multi);
194 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
195 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
199 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
200 isl_space_copy(multi->space));
201 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
204 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
205 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
209 domain = isl_space_domain(isl_space_copy(space));
210 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
213 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
214 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
215 __isl_take isl_id *id)
219 multi = FN(MULTI(BASE),cow)(multi);
221 return isl_id_free(id);
223 space = FN(MULTI(BASE),get_space)(multi);
224 space = isl_space_set_tuple_id(space, type, id);
226 return FN(MULTI(BASE),reset_space)(multi, space);
229 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
230 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
234 multi = FN(MULTI(BASE),cow)(multi);
238 for (i = 0; i < multi->n; ++i) {
239 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
240 isl_reordering_copy(exp));
245 multi = FN(MULTI(BASE),reset_domain_space)(multi,
246 isl_space_copy(exp->dim));
248 isl_reordering_free(exp);
251 isl_reordering_free(exp);
252 FN(MULTI(BASE),free)(multi);
256 /* Align the parameters of "multi" to those of "model".
258 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
259 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
263 if (!multi || !model)
266 ctx = isl_space_get_ctx(model);
267 if (!isl_space_has_named_params(model))
268 isl_die(ctx, isl_error_invalid,
269 "model has unnamed parameters", goto error);
270 if (!isl_space_has_named_params(multi->space))
271 isl_die(ctx, isl_error_invalid,
272 "input has unnamed parameters", goto error);
273 if (!isl_space_match(multi->space, isl_dim_param,
274 model, isl_dim_param)) {
277 model = isl_space_params(model);
278 exp = isl_parameter_alignment_reordering(multi->space, model);
279 exp = isl_reordering_extend_space(exp,
280 FN(MULTI(BASE),get_domain_space)(multi));
281 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
284 isl_space_free(model);
287 isl_space_free(model);
288 FN(MULTI(BASE),free)(multi);
292 static __isl_give MULTI(BASE) *align_params_multi_set_and(
293 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
294 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
295 __isl_take isl_set *set))
301 if (isl_space_match(multi->space, isl_dim_param,
302 set->dim, isl_dim_param))
303 return fn(multi, set);
304 ctx = FN(MULTI(BASE),get_ctx)(multi);
305 if (!isl_space_has_named_params(multi->space) ||
306 !isl_space_has_named_params(set->dim))
307 isl_die(ctx, isl_error_invalid,
308 "unaligned unnamed parameters", goto error);
309 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
310 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
311 return fn(multi, set);
313 FN(MULTI(BASE),free)(multi);
318 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
319 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
323 if (!multi || !context)
326 for (i = 0; i < multi->n; ++i) {
327 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
332 isl_set_free(context);
335 isl_set_free(context);
336 FN(MULTI(BASE),free)(multi);
340 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
341 __isl_take isl_set *context)
343 return align_params_multi_set_and(multi, context,
344 &FN(MULTI(BASE),gist_aligned));
347 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
348 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
350 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
351 isl_set *dom_context = isl_set_universe(space);
352 dom_context = isl_set_intersect_params(dom_context, context);
353 return FN(MULTI(BASE),gist)(multi, dom_context);
356 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
357 __isl_take isl_space *space, __isl_take LIST(EL) *list)
367 ctx = isl_space_get_ctx(space);
368 n = FN(FN(LIST(EL),n),BASE)(list);
369 if (n != isl_space_dim(space, isl_dim_out))
370 isl_die(ctx, isl_error_invalid,
371 "invalid number of elements in list", goto error);
373 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
374 for (i = 0; i < n; ++i) {
375 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
376 FN(FN(LIST(EL),get),BASE)(list, i));
379 isl_space_free(space);
380 FN(LIST(EL),free)(list);
383 isl_space_free(space);
384 FN(LIST(EL),free)(list);