isl_basic_set_sample: perform basis reduction at each level by default
[isl.git] / isl_multi_templ.c
blob8949f3bac39edf50f0eec3842ea97963c2db8e87
1 /*
2 * Copyright 2011 Sven Verdoolaege
4 * Use of this software is governed by the GNU LGPLv2.1 license
5 */
7 #define xCAT(A,B) A ## B
8 #define CAT(A,B) xCAT(A,B)
9 #undef EL
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)
37 isl_ctx *ctx;
38 int n;
39 MULTI(BASE) *multi;
41 if (!space)
42 return NULL;
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 *));
48 if (!multi)
49 goto error;
51 multi->space = space;
52 multi->n = n;
53 multi->ref = 1;
54 return multi;
55 error:
56 isl_space_free(space);
57 return NULL;
60 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
62 int i;
63 MULTI(BASE) *dup;
65 if (!multi)
66 return NULL;
68 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
69 if (!dup)
70 return NULL;
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]));
76 return dup;
79 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
81 if (!multi)
82 return NULL;
84 if (multi->ref == 1)
85 return multi;
87 multi->ref--;
88 return FN(MULTI(BASE),dup)(multi);
91 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
93 if (!multi)
94 return NULL;
96 multi->ref++;
97 return multi;
100 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
102 int i;
104 if (!multi)
105 return NULL;
107 if (--multi->ref > 0)
108 return NULL;
110 isl_space_free(multi->space);
111 for (i = 0; i < multi->n; ++i)
112 FN(EL,free)(multi->p[i]);
113 free(multi);
115 return NULL;
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,
131 int pos)
133 isl_ctx *ctx;
135 if (!multi)
136 return NULL;
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)
147 multi = FN(MULTI(BASE),cow)(multi);
148 if (!multi || !el)
149 goto error;
151 if (pos < 0 || pos >= multi->n)
152 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
153 "index out of bounds", goto error);
155 FN(EL,free)(multi->p[pos]);
156 multi->p[pos] = el;
158 return multi;
159 error:
160 FN(MULTI(BASE),free)(multi);
161 FN(EL,free)(el);
162 return NULL;
165 /* Reset the space of "multi". This function is called from isl_pw_templ.c
166 * and doesn't know if the space of an element object is represented
167 * directly or through its domain. It therefore passes along both,
168 * which we pass along to the element function since we don't how
169 * that is represented either.
171 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
172 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
173 __isl_take isl_space *domain)
175 int i;
177 multi = FN(MULTI(BASE),cow)(multi);
178 if (!multi || !space || !domain)
179 goto error;
181 for (i = 0; i < multi->n; ++i) {
182 multi->p[i] = FN(EL,reset_space_and_domain)(multi->p[i],
183 isl_space_copy(space), isl_space_copy(domain));
184 if (!multi->p[i])
185 goto error;
187 isl_space_free(domain);
188 isl_space_free(multi->space);
189 multi->space = space;
191 return multi;
192 error:
193 isl_space_free(domain);
194 isl_space_free(space);
195 FN(MULTI(BASE),free)(multi);
196 return NULL;
199 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
200 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
202 isl_space *space;
204 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
205 isl_space_copy(multi->space));
206 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
209 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
210 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
212 isl_space *domain;
214 domain = isl_space_domain(isl_space_copy(space));
215 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
218 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
219 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
220 __isl_take isl_id *id)
222 isl_space *space;
224 multi = FN(MULTI(BASE),cow)(multi);
225 if (!multi)
226 return isl_id_free(id);
228 space = FN(MULTI(BASE),get_space)(multi);
229 space = isl_space_set_tuple_id(space, type, id);
231 return FN(MULTI(BASE),reset_space)(multi, space);
234 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
235 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
237 int i;
239 multi = FN(MULTI(BASE),cow)(multi);
240 if (!multi || !exp)
241 return NULL;
243 for (i = 0; i < multi->n; ++i) {
244 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
245 isl_reordering_copy(exp));
246 if (!multi->p[i])
247 goto error;
250 multi = FN(MULTI(BASE),reset_domain_space)(multi,
251 isl_space_copy(exp->dim));
253 isl_reordering_free(exp);
254 return multi;
255 error:
256 isl_reordering_free(exp);
257 FN(MULTI(BASE),free)(multi);
258 return NULL;
261 /* Align the parameters of "multi" to those of "model".
263 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
264 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
266 isl_ctx *ctx;
268 if (!multi || !model)
269 goto error;
271 ctx = isl_space_get_ctx(model);
272 if (!isl_space_has_named_params(model))
273 isl_die(ctx, isl_error_invalid,
274 "model has unnamed parameters", goto error);
275 if (!isl_space_has_named_params(multi->space))
276 isl_die(ctx, isl_error_invalid,
277 "input has unnamed parameters", goto error);
278 if (!isl_space_match(multi->space, isl_dim_param,
279 model, isl_dim_param)) {
280 isl_reordering *exp;
282 model = isl_space_params(model);
283 exp = isl_parameter_alignment_reordering(multi->space, model);
284 exp = isl_reordering_extend_space(exp,
285 FN(MULTI(BASE),get_domain_space)(multi));
286 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
289 isl_space_free(model);
290 return multi;
291 error:
292 isl_space_free(model);
293 FN(MULTI(BASE),free)(multi);
294 return NULL;
297 static __isl_give MULTI(BASE) *align_params_multi_set_and(
298 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
299 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
300 __isl_take isl_set *set))
302 isl_ctx *ctx;
304 if (!multi || !set)
305 goto error;
306 if (isl_space_match(multi->space, isl_dim_param,
307 set->dim, isl_dim_param))
308 return fn(multi, set);
309 ctx = FN(MULTI(BASE),get_ctx)(multi);
310 if (!isl_space_has_named_params(multi->space) ||
311 !isl_space_has_named_params(set->dim))
312 isl_die(ctx, isl_error_invalid,
313 "unaligned unnamed parameters", goto error);
314 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
315 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
316 return fn(multi, set);
317 error:
318 FN(MULTI(BASE),free)(multi);
319 isl_set_free(set);
320 return NULL;
323 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
324 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
326 int i;
328 if (!multi || !context)
329 goto error;
331 for (i = 0; i < multi->n; ++i) {
332 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
333 if (!multi->p[i])
334 goto error;
337 isl_set_free(context);
338 return multi;
339 error:
340 isl_set_free(context);
341 FN(MULTI(BASE),free)(multi);
342 return NULL;
345 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
346 __isl_take isl_set *context)
348 return align_params_multi_set_and(multi, context,
349 &FN(MULTI(BASE),gist_aligned));
352 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
353 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
355 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
356 isl_set *dom_context = isl_set_universe(space);
357 dom_context = isl_set_intersect_params(dom_context, context);
358 return FN(MULTI(BASE),gist)(multi, dom_context);
361 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
362 __isl_take isl_space *space, __isl_take LIST(EL) *list)
364 int i;
365 int n;
366 isl_ctx *ctx;
367 MULTI(BASE) *multi;
369 if (!space || !list)
370 goto error;
372 ctx = isl_space_get_ctx(space);
373 n = FN(FN(LIST(EL),n),BASE)(list);
374 if (n != isl_space_dim(space, isl_dim_out))
375 isl_die(ctx, isl_error_invalid,
376 "invalid number of elements in list", goto error);
378 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
379 for (i = 0; i < n; ++i) {
380 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
381 FN(FN(LIST(EL),get),BASE)(list, i));
384 isl_space_free(space);
385 FN(LIST(EL),free)(list);
386 return multi;
387 error:
388 isl_space_free(space);
389 FN(LIST(EL),free)(list);
390 return NULL;