2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 #define xCAT(A,B) A ## B
12 #define CAT(A,B) xCAT(A,B)
14 #define EL CAT(isl_,BASE)
15 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
16 #define FN(TYPE,NAME) xFN(TYPE,NAME)
17 #define xMULTI(BASE) isl_multi_ ## BASE
18 #define MULTI(BASE) xMULTI(BASE)
19 #define MULTI_NAME(BASE) "isl_multi_" #BASE
20 #define xLIST(EL) EL ## _list
21 #define LIST(EL) xLIST(EL)
23 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
25 return multi ? isl_space_get_ctx(multi->space) : NULL;
28 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
30 return multi ? isl_space_copy(multi->space) : NULL;
33 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
34 __isl_keep MULTI(BASE) *multi)
36 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
39 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
48 ctx = isl_space_get_ctx(space);
49 n = isl_space_dim(space, isl_dim_out);
50 multi = isl_calloc(ctx, MULTI(BASE),
51 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
60 isl_space_free(space);
64 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
72 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
76 for (i = 0; i < multi->n; ++i)
77 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
78 FN(EL,copy)(multi->p[i]));
83 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
92 return FN(MULTI(BASE),dup)(multi);
95 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
104 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
111 if (--multi->ref > 0)
114 isl_space_free(multi->space);
115 for (i = 0; i < multi->n; ++i)
116 FN(EL,free)(multi->p[i]);
122 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
123 enum isl_dim_type type)
125 return multi ? isl_space_dim(multi->space, type) : 0;
128 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
129 __isl_take MULTI(BASE) *multi,
130 enum isl_dim_type type, unsigned pos, const char *s)
134 multi = FN(MULTI(BASE),cow)(multi);
138 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
140 return FN(MULTI(BASE),free)(multi);
142 if (type == isl_dim_out)
144 for (i = 0; i < multi->n; ++i) {
145 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
147 return FN(MULTI(BASE),free)(multi);
153 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
154 enum isl_dim_type type)
156 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
159 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
166 ctx = FN(MULTI(BASE),get_ctx)(multi);
167 if (pos < 0 || pos >= multi->n)
168 isl_die(ctx, isl_error_invalid,
169 "index out of bounds", return NULL);
170 return FN(EL,copy)(multi->p[pos]);
173 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
174 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
176 isl_space *multi_space = NULL;
177 isl_space *el_space = NULL;
179 multi = FN(MULTI(BASE),cow)(multi);
183 multi_space = FN(MULTI(BASE),get_space)(multi);
184 el_space = FN(EL,get_space)(el);
186 if (!isl_space_match(multi_space, isl_dim_param,
187 el_space, isl_dim_param))
188 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
189 "parameters don't match", goto error);
190 if (!isl_space_tuple_match(multi_space, isl_dim_in,
191 el_space, isl_dim_in))
192 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
193 "domains don't match", goto error);
195 if (pos < 0 || pos >= multi->n)
196 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
197 "index out of bounds", goto error);
199 FN(EL,free)(multi->p[pos]);
202 isl_space_free(multi_space);
203 isl_space_free(el_space);
207 FN(MULTI(BASE),free)(multi);
209 isl_space_free(multi_space);
210 isl_space_free(el_space);
214 /* Reset the space of "multi". This function is called from isl_pw_templ.c
215 * and doesn't know if the space of an element object is represented
216 * directly or through its domain. It therefore passes along both,
217 * which we pass along to the element function since we don't how
218 * that is represented either.
220 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
221 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
222 __isl_take isl_space *domain)
226 multi = FN(MULTI(BASE),cow)(multi);
227 if (!multi || !space || !domain)
230 for (i = 0; i < multi->n; ++i) {
231 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
232 isl_space_copy(domain));
236 isl_space_free(domain);
237 isl_space_free(multi->space);
238 multi->space = space;
242 isl_space_free(domain);
243 isl_space_free(space);
244 FN(MULTI(BASE),free)(multi);
248 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
249 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
253 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
254 isl_space_copy(multi->space));
255 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
258 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
259 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
263 domain = isl_space_domain(isl_space_copy(space));
264 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
267 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
268 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
269 __isl_take isl_id *id)
273 multi = FN(MULTI(BASE),cow)(multi);
275 return isl_id_free(id);
277 space = FN(MULTI(BASE),get_space)(multi);
278 space = isl_space_set_tuple_id(space, type, id);
280 return FN(MULTI(BASE),reset_space)(multi, space);
283 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
284 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
288 multi = FN(MULTI(BASE),cow)(multi);
292 for (i = 0; i < multi->n; ++i) {
293 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
294 isl_reordering_copy(exp));
299 multi = FN(MULTI(BASE),reset_domain_space)(multi,
300 isl_space_copy(exp->dim));
302 isl_reordering_free(exp);
305 isl_reordering_free(exp);
306 FN(MULTI(BASE),free)(multi);
310 /* Align the parameters of "multi" to those of "model".
312 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
313 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
317 if (!multi || !model)
320 ctx = isl_space_get_ctx(model);
321 if (!isl_space_has_named_params(model))
322 isl_die(ctx, isl_error_invalid,
323 "model has unnamed parameters", goto error);
324 if (!isl_space_has_named_params(multi->space))
325 isl_die(ctx, isl_error_invalid,
326 "input has unnamed parameters", goto error);
327 if (!isl_space_match(multi->space, isl_dim_param,
328 model, isl_dim_param)) {
331 model = isl_space_params(model);
332 exp = isl_parameter_alignment_reordering(multi->space, model);
333 exp = isl_reordering_extend_space(exp,
334 FN(MULTI(BASE),get_domain_space)(multi));
335 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
338 isl_space_free(model);
341 isl_space_free(model);
342 FN(MULTI(BASE),free)(multi);
346 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
347 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
348 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
349 __isl_take isl_set *set))
355 if (isl_space_match(multi->space, isl_dim_param,
356 set->dim, isl_dim_param))
357 return fn(multi, set);
358 ctx = FN(MULTI(BASE),get_ctx)(multi);
359 if (!isl_space_has_named_params(multi->space) ||
360 !isl_space_has_named_params(set->dim))
361 isl_die(ctx, isl_error_invalid,
362 "unaligned unnamed parameters", goto error);
363 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
364 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
365 return fn(multi, set);
367 FN(MULTI(BASE),free)(multi);
372 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
373 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
377 if (!multi || !context)
380 for (i = 0; i < multi->n; ++i) {
381 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
386 isl_set_free(context);
389 isl_set_free(context);
390 FN(MULTI(BASE),free)(multi);
394 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
395 __isl_take isl_set *context)
397 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
398 &FN(MULTI(BASE),gist_aligned));
401 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
402 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
404 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
405 isl_set *dom_context = isl_set_universe(space);
406 dom_context = isl_set_intersect_params(dom_context, context);
407 return FN(MULTI(BASE),gist)(multi, dom_context);
410 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
411 __isl_take isl_space *space, __isl_take LIST(EL) *list)
421 ctx = isl_space_get_ctx(space);
422 n = FN(FN(LIST(EL),n),BASE)(list);
423 if (n != isl_space_dim(space, isl_dim_out))
424 isl_die(ctx, isl_error_invalid,
425 "invalid number of elements in list", goto error);
427 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
428 for (i = 0; i < n; ++i) {
429 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
430 FN(FN(LIST(EL),get),BASE)(list, i));
433 isl_space_free(space);
434 FN(LIST(EL),free)(list);
437 isl_space_free(space);
438 FN(LIST(EL),free)(list);
442 /* Create a multi expression in the given space that maps each
443 * input dimension to the corresponding output dimension.
445 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
454 if (isl_space_is_set(space))
455 isl_die(isl_space_get_ctx(space), isl_error_invalid,
456 "expecting map space", goto error);
458 n = isl_space_dim(space, isl_dim_out);
459 if (n != isl_space_dim(space, isl_dim_in))
460 isl_die(isl_space_get_ctx(space), isl_error_invalid,
461 "number of input and output dimensions needs to be "
462 "the same", goto error);
464 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
467 isl_space_free(space);
471 space = isl_space_domain(space);
472 ls = isl_local_space_from_space(space);
474 for (i = 0; i < n; ++i) {
476 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
478 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
481 isl_local_space_free(ls);
485 isl_space_free(space);
489 /* Construct a multi expression in the given space with value zero in
490 * each of the output dimensions.
492 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
500 n = isl_space_dim(space , isl_dim_out);
501 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
504 isl_space_free(space);
510 space = isl_space_domain(space);
511 ls = isl_local_space_from_space(space);
512 el = FN(EL,zero_on_domain)(ls);
514 for (i = 0; i < n; ++i)
515 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
524 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
525 __isl_take MULTI(BASE) *multi,
526 enum isl_dim_type type, unsigned first, unsigned n)
531 multi = FN(MULTI(BASE),cow)(multi);
535 dim = FN(MULTI(BASE),dim)(multi, type);
536 if (first + n > dim || first + n < first)
537 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
538 "index out of bounds",
539 return FN(MULTI(BASE),cow)(multi));
541 multi->space = isl_space_drop_dims(multi->space, type, first, n);
543 return FN(MULTI(BASE),cow)(multi);
545 if (type == isl_dim_out) {
546 for (i = 0; i < n; ++i)
547 FN(EL,free)(multi->p[first + i]);
548 for (i = first; i + n < multi->n; ++i)
549 multi->p[i] = multi->p[i + n];
555 for (i = 0; i < multi->n; ++i) {
556 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
558 return FN(MULTI(BASE),cow)(multi);
564 /* Given two MULTI(BASE)s A -> B and C -> D,
565 * construct a MULTI(BASE) (A * C) -> (B, D).
567 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
568 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
575 if (!multi1 || !multi2)
578 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
579 FN(MULTI(BASE),get_space)(multi2));
580 space = isl_space_flatten_range(space);
581 res = FN(MULTI(BASE),alloc)(space);
583 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
584 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
586 for (i = 0; i < n1; ++i) {
587 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
588 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
591 for (i = 0; i < n2; ++i) {
592 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
593 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
596 FN(MULTI(BASE),free)(multi1);
597 FN(MULTI(BASE),free)(multi2);
600 FN(MULTI(BASE),free)(multi1);
601 FN(MULTI(BASE),free)(multi2);