isl_output.c: print_affine: drop isl_basic_map argument
[isl.git] / isl_union_templ.c
blob85f0570b07cc90dbdb1e4e8d958da9ffc3406c03
1 /*
2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
15 isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
17 return u ? u->space->ctx : NULL;
20 __isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
22 if (!u)
23 return NULL;
24 return isl_space_copy(u->space);
27 /* Return the number of parameters of "u", where "type"
28 * is required to be set to isl_dim_param.
30 unsigned FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
32 if (!u)
33 return 0;
35 if (type != isl_dim_param)
36 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
37 "can only reference parameters", return 0);
39 return isl_space_dim(u->space, type);
42 /* Return the position of the parameter with the given name
43 * in "u".
44 * Return -1 if no such dimension can be found.
46 int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
47 const char *name)
49 if (!u)
50 return -1;
51 return isl_space_find_dim_by_name(u->space, type, name);
54 #ifdef HAS_TYPE
55 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim,
56 enum isl_fold type, int size)
57 #else
58 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim, int size)
59 #endif
61 UNION *u;
63 dim = isl_space_params(dim);
64 if (!dim)
65 return NULL;
67 u = isl_calloc_type(dim->ctx, UNION);
68 if (!u)
69 goto error;
71 u->ref = 1;
72 #ifdef HAS_TYPE
73 u->type = type;
74 #endif
75 u->space = dim;
76 if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
77 return FN(UNION,free)(u);
79 return u;
80 error:
81 isl_space_free(dim);
82 return NULL;
85 #ifdef HAS_TYPE
86 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
88 return FN(UNION,alloc)(dim, type, 16);
90 #else
91 __isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim)
93 return FN(UNION,alloc)(dim, 16);
95 #endif
97 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
99 if (!u)
100 return NULL;
102 u->ref++;
103 return u;
106 /* Extract the element of "u" living in "space" (ignoring parameters).
108 * Return the ZERO element if "u" does not contain any element
109 * living in "space".
111 __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
112 __isl_take isl_space *space)
114 struct isl_hash_table_entry *entry;
115 isl_bool equal_params;
117 if (!u || !space)
118 goto error;
119 equal_params = isl_space_has_equal_params(u->space, space);
120 if (equal_params < 0)
121 goto error;
122 if (!equal_params) {
123 space = isl_space_drop_dims(space, isl_dim_param,
124 0, isl_space_dim(space, isl_dim_param));
125 space = isl_space_align_params(space,
126 FN(UNION,get_space)(u));
127 if (!space)
128 goto error;
131 entry = FN(UNION,find_part_entry)(u, space, 0);
132 if (!entry)
133 goto error;
134 if (entry == isl_hash_table_entry_none)
135 #ifdef HAS_TYPE
136 return FN(PART,ZERO)(space, u->type);
137 #else
138 return FN(PART,ZERO)(space);
139 #endif
140 isl_space_free(space);
141 return FN(PART,copy)(entry->data);
142 error:
143 isl_space_free(space);
144 return NULL;
147 /* Add "part" to "u".
148 * If "disjoint" is set, then "u" is not allowed to already have
149 * a part that is defined over a domain that overlaps with the domain
150 * of "part".
151 * Otherwise, compute the union sum of "part" and the part in "u"
152 * defined on the same space.
154 static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
155 __isl_take PART *part, int disjoint)
157 int empty;
158 struct isl_hash_table_entry *entry;
160 if (!part)
161 goto error;
163 empty = FN(PART,IS_ZERO)(part);
164 if (empty < 0)
165 goto error;
166 if (empty) {
167 FN(PART,free)(part);
168 return u;
171 u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
172 part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
174 u = FN(UNION,cow)(u);
176 if (!u)
177 goto error;
179 if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
180 goto error;
181 entry = FN(UNION,find_part_entry)(u, part->dim, 1);
182 if (!entry)
183 goto error;
185 if (!entry->data)
186 entry->data = part;
187 else {
188 if (disjoint &&
189 FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
190 goto error;
191 entry->data = FN(PART,union_add_)(entry->data,
192 FN(PART,copy)(part));
193 if (!entry->data)
194 goto error;
195 empty = FN(PART,IS_ZERO)(part);
196 if (empty < 0)
197 goto error;
198 if (empty)
199 u = FN(UNION,remove_part_entry)(u, entry);
200 FN(PART,free)(part);
203 return u;
204 error:
205 FN(PART,free)(part);
206 FN(UNION,free)(u);
207 return NULL;
210 /* Add "part" to "u", where "u" is assumed not to already have
211 * a part that is defined on the same space as "part".
213 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
214 __isl_take PART *part)
216 return FN(UNION,add_part_generic)(u, part, 1);
219 #ifdef HAS_TYPE
220 /* Allocate a UNION with the same type and the same size as "u" and
221 * with space "space".
223 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
224 __isl_take isl_space *space)
226 if (!u)
227 goto error;
228 return FN(UNION,alloc)(space, u->type, u->table.n);
229 error:
230 isl_space_free(space);
231 return NULL;
233 #else
234 /* Allocate a UNION with the same size as "u" and with space "space".
236 static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
237 __isl_take isl_space *space)
239 if (!u)
240 goto error;
241 return FN(UNION,alloc)(space, u->table.n);
242 error:
243 isl_space_free(space);
244 return NULL;
246 #endif
248 /* Allocate a UNION with the same space, the same type (if any) and
249 * the same size as "u".
251 static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
253 return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
256 /* Internal data structure for isl_union_*_transform_space.
257 * "fn' is applied to each entry in the input.
258 * "res" collects the results.
260 S(UNION,transform_data)
262 __isl_give PART *(*fn)(__isl_take PART *part, void *user);
263 void *user;
265 UNION *res;
268 /* Apply data->fn to "part" and add the result to data->res.
270 static isl_stat FN(UNION,transform_entry)(__isl_take PART *part, void *user)
272 S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
274 part = data->fn(part, data->user);
275 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
276 if (!data->res)
277 return isl_stat_error;
279 return isl_stat_ok;
282 /* Return a UNION living in "space" that is obtained by applying "fn"
283 * to each of the entries in "u".
285 static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
286 isl_space *space,
287 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
289 S(UNION,transform_data) data = { fn, user };
291 data.res = FN(UNION,alloc_same_size_on_space)(u, space);
292 if (FN(FN(UNION,foreach),PARTS)(u,
293 &FN(UNION,transform_entry), &data) < 0)
294 data.res = FN(UNION,free)(data.res);
295 FN(UNION,free)(u);
296 return data.res;
299 /* Return a UNION that lives in the same space as "u" and that is obtained
300 * by applying "fn" to each of the entries in "u".
302 static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
303 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
305 return FN(UNION,transform_space)(u, FN(UNION,get_space)(u), fn, user);
308 /* Apply data->fn to *part and store the result back into *part.
310 static isl_stat FN(UNION,transform_inplace_entry)(void **part, void *user)
312 S(UNION,transform_data) *data = (S(UNION,transform_data) *) user;
314 *part = data->fn(*part, data->user);
315 if (!*part)
316 return isl_stat_error;
317 return isl_stat_ok;
320 /* Update "u" by applying "fn" to each entry.
321 * This operation is assumed not to change the number of entries nor
322 * the spaces of the entries.
324 * If there is only one reference to "u", then change "u" inplace.
325 * Otherwise, create a new UNION from "u" and discard the original.
327 static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
328 __isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
330 isl_bool single_ref;
332 single_ref = FN(UNION,has_single_reference)(u);
333 if (single_ref < 0)
334 return FN(UNION,free)(u);
335 if (single_ref) {
336 S(UNION,transform_data) data = { fn, user };
337 if (FN(UNION,foreach_inplace)(u,
338 &FN(UNION,transform_inplace_entry), &data) < 0)
339 return FN(UNION,free)(u);
340 return u;
342 return FN(UNION,transform)(u, fn, user);
345 /* An isl_union_*_transform callback for use in isl_union_*_dup
346 * that simply returns "part".
348 static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
350 return part;
353 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
355 u = FN(UNION,copy)(u);
356 return FN(UNION,transform)(u, &FN(UNION,copy_part), NULL);
359 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
361 if (!u)
362 return NULL;
364 if (u->ref == 1)
365 return u;
366 u->ref--;
367 return FN(UNION,dup)(u);
370 __isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
372 if (!u)
373 return NULL;
375 if (--u->ref > 0)
376 return NULL;
378 isl_hash_table_foreach(u->space->ctx, &u->table,
379 &FN(UNION,free_u_entry), NULL);
380 isl_hash_table_clear(&u->table);
381 isl_space_free(u->space);
382 free(u);
383 return NULL;
386 static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
388 isl_reordering *exp = user;
390 exp = isl_reordering_extend_space(isl_reordering_copy(exp),
391 FN(PART,get_domain_space)(part));
392 return FN(PART,realign_domain)(part, exp);
395 /* Reorder the parameters of "u" according to the given reordering.
397 static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
398 __isl_take isl_reordering *r)
400 isl_space *space;
402 if (!u || !r)
403 goto error;
405 space = isl_space_copy(r->dim);
406 u = FN(UNION,transform_space)(u, space, &FN(UNION,align_entry), r);
407 isl_reordering_free(r);
408 return u;
409 error:
410 FN(UNION,free)(u);
411 isl_reordering_free(r);
412 return NULL;
415 /* Align the parameters of "u" to those of "model".
417 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
418 __isl_take isl_space *model)
420 isl_bool equal_params;
421 isl_reordering *r;
423 if (!u || !model)
424 goto error;
426 equal_params = isl_space_has_equal_params(u->space, model);
427 if (equal_params < 0)
428 goto error;
429 if (equal_params) {
430 isl_space_free(model);
431 return u;
434 model = isl_space_params(model);
435 r = isl_parameter_alignment_reordering(u->space, model);
436 isl_space_free(model);
438 return FN(UNION,realign_domain)(u, r);
439 error:
440 isl_space_free(model);
441 FN(UNION,free)(u);
442 return NULL;
445 /* Add "part" to *u, taking the union sum if "u" already has
446 * a part defined on the same space as "part".
448 static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
450 UNION **u = (UNION **)user;
452 *u = FN(UNION,add_part_generic)(*u, part, 0);
454 return isl_stat_ok;
457 /* Compute the sum of "u1" and "u2" on the union of their domains,
458 * with the actual sum on the shared domain and
459 * the defined expression on the symmetric difference of the domains.
461 * This is an internal function that is exposed under different
462 * names depending on whether the base expressions have a zero default
463 * value.
464 * If they do, then this function is called "add".
465 * Otherwise, it is called "union_add".
467 static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
468 __isl_take UNION *u2)
470 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
471 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
473 u1 = FN(UNION,cow)(u1);
475 if (!u1 || !u2)
476 goto error;
478 if (FN(FN(UNION,foreach),PARTS)(u2, &FN(UNION,union_add_part), &u1) < 0)
479 goto error;
481 FN(UNION,free)(u2);
483 return u1;
484 error:
485 FN(UNION,free)(u1);
486 FN(UNION,free)(u2);
487 return NULL;
490 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
492 isl_space *dim;
493 UNION *u;
495 if (!part)
496 return NULL;
498 dim = FN(PART,get_space)(part);
499 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
500 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
501 #ifdef HAS_TYPE
502 u = FN(UNION,ZERO)(dim, part->type);
503 #else
504 u = FN(UNION,ZERO)(dim);
505 #endif
506 u = FN(FN(UNION,add),PARTS)(u, part);
508 return u;
511 S(UNION,match_bin_data) {
512 UNION *u2;
513 UNION *res;
514 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
517 /* Check if data->u2 has an element living in the same space as "part".
518 * If so, call data->fn on the two elements and add the result to
519 * data->res.
521 static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
523 S(UNION,match_bin_data) *data = user;
524 struct isl_hash_table_entry *entry2;
525 isl_space *space;
526 PART *part2;
528 space = FN(PART,get_space)(part);
529 entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
530 isl_space_free(space);
531 if (!entry2)
532 goto error;
533 if (entry2 == isl_hash_table_entry_none) {
534 FN(PART,free)(part);
535 return isl_stat_ok;
538 part2 = entry2->data;
539 if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
540 part2->dim, isl_dim_out))
541 isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
542 "entries should have the same range space",
543 goto error);
545 part = data->fn(part, FN(PART, copy)(entry2->data));
547 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
548 if (!data->res)
549 return isl_stat_error;
551 return isl_stat_ok;
552 error:
553 FN(PART,free)(part);
554 return isl_stat_error;
557 /* This function is currently only used from isl_polynomial.c
558 * and not from isl_fold.c.
560 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
561 __isl_take UNION *u2,
562 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
563 __attribute__ ((unused));
564 /* For each pair of elements in "u1" and "u2" living in the same space,
565 * call "fn" and collect the results.
567 static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
568 __isl_take UNION *u2,
569 __isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
571 S(UNION,match_bin_data) data = { NULL, NULL, fn };
573 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
574 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
576 if (!u1 || !u2)
577 goto error;
579 data.u2 = u2;
580 data.res = FN(UNION,alloc_same_size)(u1);
581 if (FN(FN(UNION,foreach),PARTS)(u1,
582 &FN(UNION,match_bin_entry), &data) < 0)
583 goto error;
585 FN(UNION,free)(u1);
586 FN(UNION,free)(u2);
587 return data.res;
588 error:
589 FN(UNION,free)(u1);
590 FN(UNION,free)(u2);
591 FN(UNION,free)(data.res);
592 return NULL;
595 /* Compute the sum of "u1" and "u2".
597 * If the base expressions have a default zero value, then the sum
598 * is computed on the union of the domains of "u1" and "u2".
599 * Otherwise, it is computed on their shared domains.
601 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
603 #if DEFAULT_IS_ZERO
604 return FN(UNION,union_add_)(u1, u2);
605 #else
606 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
607 #endif
610 #ifndef NO_SUB
611 /* Subtract "u2" from "u1" and return the result.
613 __isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
615 return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
617 #endif
619 S(UNION,any_set_data) {
620 isl_set *set;
621 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
624 static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
625 void *user)
627 S(UNION,any_set_data) *data = user;
629 return data->fn(part, isl_set_copy(data->set));
632 /* Update each element of "u" by calling "fn" on the element and "set".
634 static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
635 __isl_take isl_set *set,
636 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
638 S(UNION,any_set_data) data = { NULL, fn };
640 u = FN(UNION,align_params)(u, isl_set_get_space(set));
641 set = isl_set_align_params(set, FN(UNION,get_space)(u));
643 if (!u || !set)
644 goto error;
646 data.set = set;
647 u = FN(UNION,transform)(u, &FN(UNION,any_set_entry), &data);
648 isl_set_free(set);
649 return u;
650 error:
651 FN(UNION,free)(u);
652 isl_set_free(set);
653 return NULL;
656 /* Intersect the domain of "u" with the parameter domain "context".
658 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
659 __isl_take isl_set *set)
661 return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
664 /* Compute the gist of the domain of "u" with respect to
665 * the parameter domain "context".
667 __isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
668 __isl_take isl_set *set)
670 return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
673 S(UNION,match_domain_data) {
674 isl_union_set *uset;
675 UNION *res;
676 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
679 static int FN(UNION,set_has_dim)(const void *entry, const void *val)
681 isl_set *set = (isl_set *)entry;
682 isl_space *dim = (isl_space *)val;
684 return isl_space_is_equal(set->dim, dim);
687 /* Find the set in data->uset that lives in the same space as the domain
688 * of "part", apply data->fn to *entry and this set (if any), and add
689 * the result to data->res.
691 static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
693 S(UNION,match_domain_data) *data = user;
694 uint32_t hash;
695 struct isl_hash_table_entry *entry2;
696 isl_space *space;
698 space = FN(PART,get_domain_space)(part);
699 hash = isl_space_get_hash(space);
700 entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
701 hash, &FN(UNION,set_has_dim), space, 0);
702 isl_space_free(space);
703 if (!entry2) {
704 FN(PART,free)(part);
705 return isl_stat_ok;
708 part = data->fn(part, isl_set_copy(entry2->data));
710 data->res = FN(FN(UNION,add),PARTS)(data->res, part);
711 if (!data->res)
712 return isl_stat_error;
714 return isl_stat_ok;
717 /* Apply fn to each pair of PW in u and set in uset such that
718 * the set lives in the same space as the domain of PW
719 * and collect the results.
721 static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
722 __isl_take isl_union_set *uset,
723 __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
725 S(UNION,match_domain_data) data = { NULL, NULL, fn };
727 u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
728 uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
730 if (!u || !uset)
731 goto error;
733 data.uset = uset;
734 data.res = FN(UNION,alloc_same_size)(u);
735 if (FN(FN(UNION,foreach),PARTS)(u,
736 &FN(UNION,match_domain_entry), &data) < 0)
737 goto error;
739 FN(UNION,free)(u);
740 isl_union_set_free(uset);
741 return data.res;
742 error:
743 FN(UNION,free)(u);
744 isl_union_set_free(uset);
745 FN(UNION,free)(data.res);
746 return NULL;
749 /* Intersect the domain of "u" with "uset".
750 * If "uset" is a parameters domain, then intersect the parameter
751 * domain of "u" with this set.
753 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
754 __isl_take isl_union_set *uset)
756 if (isl_union_set_is_params(uset))
757 return FN(UNION,intersect_params)(u,
758 isl_set_from_union_set(uset));
759 return FN(UNION,match_domain_op)(u, uset, &FN(PW,intersect_domain));
762 /* Take the set (which may be empty) in data->uset that lives
763 * in the same space as the domain of "pw", subtract it from the domain
764 * of "part" and return the result.
766 static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
767 void *user)
769 isl_union_set *uset = user;
770 isl_space *space;
771 isl_set *set;
773 space = FN(PART,get_domain_space)(part);
774 set = isl_union_set_extract_set(uset, space);
775 return FN(PART,subtract_domain)(part, set);
778 /* Subtract "uset' from the domain of "u".
780 __isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
781 __isl_take isl_union_set *uset)
783 u = FN(UNION,transform)(u, &FN(UNION,subtract_domain_entry), uset);
784 isl_union_set_free(uset);
785 return u;
788 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
789 __isl_take isl_union_set *uset)
791 if (isl_union_set_is_params(uset))
792 return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
793 return FN(UNION,match_domain_op)(u, uset, &FN(PW,gist));
796 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
797 * Since the UNION may have several references, the entry is only
798 * replaced if the coalescing is successful.
800 static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
802 PART **part_p = (PART **) entry;
803 PART *part;
805 part = FN(PART,copy)(*part_p);
806 part = FN(PW,coalesce)(part);
807 if (!part)
808 return isl_stat_error;
809 FN(PART,free)(*part_p);
810 *part_p = part;
812 return isl_stat_ok;
815 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
817 if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
818 goto error;
820 return u;
821 error:
822 FN(UNION,free)(u);
823 return NULL;
826 static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
828 isl_union_set **uset = (isl_union_set **)user;
830 *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
832 return isl_stat_ok;
835 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
837 isl_union_set *uset;
839 uset = isl_union_set_empty(FN(UNION,get_space)(u));
840 if (FN(FN(UNION,foreach),PARTS)(u, &FN(UNION,domain_entry), &uset) < 0)
841 goto error;
843 FN(UNION,free)(u);
845 return uset;
846 error:
847 isl_union_set_free(uset);
848 FN(UNION,free)(u);
849 return NULL;
852 #ifdef HAS_TYPE
853 /* Negate the type of "u".
855 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
857 u = FN(UNION,cow)(u);
858 if (!u)
859 return NULL;
860 u->type = isl_fold_type_negate(u->type);
861 return u;
863 #else
864 /* Negate the type of "u".
865 * Since "u" does not have a type, do nothing.
867 static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
869 return u;
871 #endif
873 static __isl_give PART *FN(UNION,mul_isl_int_entry)(__isl_take PART *part,
874 void *user)
876 isl_int *v = user;
878 return FN(PW,mul_isl_int)(part, *v);
881 __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
883 if (isl_int_is_one(v))
884 return u;
886 if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
887 UNION *zero;
888 isl_space *dim = FN(UNION,get_space)(u);
889 #ifdef HAS_TYPE
890 zero = FN(UNION,ZERO)(dim, u->type);
891 #else
892 zero = FN(UNION,ZERO)(dim);
893 #endif
894 FN(UNION,free)(u);
895 return zero;
898 u = FN(UNION,transform_inplace)(u, &FN(UNION,mul_isl_int_entry), &v);
899 if (isl_int_is_neg(v))
900 u = FN(UNION,negate_type)(u);
902 return u;
905 /* Multiply "part" by the isl_val "user" and return the result.
907 static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
908 void *user)
910 isl_val *v = user;
912 return FN(PART,scale_val)(part, isl_val_copy(v));
915 /* Multiply "u" by "v" and return the result.
917 __isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
918 __isl_take isl_val *v)
920 if (!u || !v)
921 goto error;
922 if (isl_val_is_one(v)) {
923 isl_val_free(v);
924 return u;
927 if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
928 UNION *zero;
929 isl_space *space = FN(UNION,get_space)(u);
930 #ifdef HAS_TYPE
931 zero = FN(UNION,ZERO)(space, u->type);
932 #else
933 zero = FN(UNION,ZERO)(space);
934 #endif
935 FN(UNION,free)(u);
936 isl_val_free(v);
937 return zero;
940 if (!isl_val_is_rat(v))
941 isl_die(isl_val_get_ctx(v), isl_error_invalid,
942 "expecting rational factor", goto error);
944 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
945 if (isl_val_is_neg(v))
946 u = FN(UNION,negate_type)(u);
948 isl_val_free(v);
949 return u;
950 error:
951 isl_val_free(v);
952 FN(UNION,free)(u);
953 return NULL;
956 /* Divide "part" by the isl_val "user" and return the result.
958 static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
959 void *user)
961 isl_val *v = user;
963 return FN(PART,scale_down_val)(part, isl_val_copy(v));
966 /* Divide "u" by "v" and return the result.
968 __isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
969 __isl_take isl_val *v)
971 if (!u || !v)
972 goto error;
973 if (isl_val_is_one(v)) {
974 isl_val_free(v);
975 return u;
978 if (!isl_val_is_rat(v))
979 isl_die(isl_val_get_ctx(v), isl_error_invalid,
980 "expecting rational factor", goto error);
981 if (isl_val_is_zero(v))
982 isl_die(isl_val_get_ctx(v), isl_error_invalid,
983 "cannot scale down by zero", goto error);
985 u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
986 if (isl_val_is_neg(v))
987 u = FN(UNION,negate_type)(u);
989 isl_val_free(v);
990 return u;
991 error:
992 isl_val_free(v);
993 FN(UNION,free)(u);
994 return NULL;
997 S(UNION,plain_is_equal_data)
999 UNION *u2;
1000 isl_bool is_equal;
1003 static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
1005 S(UNION,plain_is_equal_data) *data = user;
1006 struct isl_hash_table_entry *entry2;
1007 PW *pw = *entry;
1009 entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
1010 if (!entry2 || entry2 == isl_hash_table_entry_none) {
1011 if (!entry2)
1012 data->is_equal = isl_bool_error;
1013 else
1014 data->is_equal = isl_bool_false;
1015 return isl_stat_error;
1018 data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
1019 if (data->is_equal < 0 || !data->is_equal)
1020 return isl_stat_error;
1022 return isl_stat_ok;
1025 isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
1027 S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
1028 int n1, n2;
1030 if (!u1 || !u2)
1031 return isl_bool_error;
1032 if (u1 == u2)
1033 return isl_bool_true;
1034 if (u1->table.n != u2->table.n)
1035 return isl_bool_false;
1036 n1 = FN(FN(UNION,n),PARTS)(u1);
1037 n2 = FN(FN(UNION,n),PARTS)(u2);
1038 if (n1 < 0 || n2 < 0)
1039 return isl_bool_error;
1040 if (n1 != n2)
1041 return isl_bool_false;
1043 u1 = FN(UNION,copy)(u1);
1044 u2 = FN(UNION,copy)(u2);
1045 u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
1046 u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
1047 if (!u1 || !u2)
1048 goto error;
1050 data.u2 = u2;
1051 if (FN(UNION,foreach_inplace)(u1,
1052 &FN(UNION,plain_is_equal_entry), &data) < 0 &&
1053 data.is_equal)
1054 goto error;
1056 FN(UNION,free)(u1);
1057 FN(UNION,free)(u2);
1059 return data.is_equal;
1060 error:
1061 FN(UNION,free)(u1);
1062 FN(UNION,free)(u2);
1063 return isl_bool_error;
1066 /* Check whether the element that "entry" points to involves any NaNs and
1067 * store the result in *nan.
1068 * Abort as soon as one such element has been found.
1070 static isl_stat FN(UNION,involves_nan_entry)(void **entry, void *user)
1072 isl_bool *nan = user;
1073 PW *pw = *entry;
1075 *nan = FN(PW,involves_nan)(pw);
1076 if (*nan < 0 || !nan)
1077 return isl_stat_error;
1079 return isl_stat_ok;
1082 /* Does "u" involve any NaNs?
1084 isl_bool FN(UNION,involves_nan)(__isl_keep UNION *u)
1086 isl_bool nan = isl_bool_false;
1088 if (!u)
1089 return isl_bool_error;
1091 if (FN(UNION,foreach_inplace)(u,
1092 &FN(UNION,involves_nan_entry), &nan) < 0 &&
1093 !nan)
1094 return isl_bool_error;
1096 return nan;
1099 /* Internal data structure for isl_union_*_drop_dims.
1100 * type, first and n are passed to isl_*_drop_dims.
1102 S(UNION,drop_dims_data) {
1103 enum isl_dim_type type;
1104 unsigned first;
1105 unsigned n;
1108 /* Drop the parameters specified by "data" from "part" and return the result.
1110 static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
1111 void *user)
1113 S(UNION,drop_dims_data) *data = user;
1115 return FN(PART,drop_dims)(part, data->type, data->first, data->n);
1118 /* Drop the specified parameters from "u".
1119 * That is, type is required to be isl_dim_param.
1121 __isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
1122 enum isl_dim_type type, unsigned first, unsigned n)
1124 isl_space *space;
1125 S(UNION,drop_dims_data) data = { type, first, n };
1127 if (!u)
1128 return NULL;
1130 if (type != isl_dim_param)
1131 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1132 "can only project out parameters",
1133 return FN(UNION,free)(u));
1135 space = FN(UNION,get_space)(u);
1136 space = isl_space_drop_dims(space, type, first, n);
1137 return FN(UNION,transform_space)(u, space, &FN(UNION,drop_dims_entry),
1138 &data);
1141 /* Internal data structure for isl_union_*_set_dim_name.
1142 * pos is the position of the parameter that needs to be renamed.
1143 * s is the new name.
1145 S(UNION,set_dim_name_data) {
1146 unsigned pos;
1147 const char *s;
1150 /* Change the name of the parameter at position data->pos of "part" to data->s
1151 * and return the result.
1153 static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
1154 void *user)
1156 S(UNION,set_dim_name_data) *data = user;
1158 return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
1161 /* Change the name of the parameter at position "pos" to "s".
1162 * That is, type is required to be isl_dim_param.
1164 __isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
1165 enum isl_dim_type type, unsigned pos, const char *s)
1167 S(UNION,set_dim_name_data) data = { pos, s };
1168 isl_space *space;
1170 if (!u)
1171 return NULL;
1173 if (type != isl_dim_param)
1174 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
1175 "can only set parameter names",
1176 return FN(UNION,free)(u));
1178 space = FN(UNION,get_space)(u);
1179 space = isl_space_set_dim_name(space, type, pos, s);
1180 return FN(UNION,transform_space)(u, space,
1181 &FN(UNION,set_dim_name_entry), &data);
1184 /* Reset the user pointer on all identifiers of parameters and tuples
1185 * of the space of "part" and return the result.
1187 static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
1188 void *user)
1190 return FN(PART,reset_user)(part);
1193 /* Reset the user pointer on all identifiers of parameters and tuples
1194 * of the spaces of "u".
1196 __isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
1198 isl_space *space;
1200 space = FN(UNION,get_space)(u);
1201 space = isl_space_reset_user(space);
1202 return FN(UNION,transform_space)(u, space, &FN(UNION,reset_user_entry),
1203 NULL);