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,
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 /* Return the space of "u".
22 static __isl_keep isl_space
*FN(UNION
,peek_space
)(__isl_keep UNION
*u
)
29 /* Return a copy of the space of "u".
31 __isl_give isl_space
*FN(UNION
,get_space
)(__isl_keep UNION
*u
)
33 return isl_space_copy(FN(UNION
,peek_space
)(u
));
36 /* Return the number of parameters of "u", where "type"
37 * is required to be set to isl_dim_param.
39 unsigned FN(UNION
,dim
)(__isl_keep UNION
*u
, enum isl_dim_type type
)
44 if (type
!= isl_dim_param
)
45 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
46 "can only reference parameters", return 0);
48 return isl_space_dim(u
->space
, type
);
51 /* Return the position of the parameter with the given name
53 * Return -1 if no such dimension can be found.
55 int FN(UNION
,find_dim_by_name
)(__isl_keep UNION
*u
, enum isl_dim_type type
,
60 return isl_space_find_dim_by_name(u
->space
, type
, name
);
64 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
,
65 enum isl_fold type
, int size
)
67 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
, int size
)
72 dim
= isl_space_params(dim
);
76 u
= isl_calloc_type(dim
->ctx
, UNION
);
85 if (isl_hash_table_init(dim
->ctx
, &u
->table
, size
) < 0)
86 return FN(UNION
,free
)(u
);
95 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
97 return FN(UNION
,alloc
)(dim
, type
, 16);
100 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
)
102 return FN(UNION
,alloc
)(dim
, 16);
106 __isl_give UNION
*FN(UNION
,copy
)(__isl_keep UNION
*u
)
115 /* Extract the element of "u" living in "space" (ignoring parameters).
117 * Return the ZERO element if "u" does not contain any element
120 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
121 __isl_take isl_space
*space
)
123 struct isl_hash_table_entry
*entry
;
125 space
= isl_space_replace_params(space
, FN(UNION
,peek_space
)(u
));
127 entry
= FN(UNION
,find_part_entry
)(u
, space
, 0);
130 if (entry
== isl_hash_table_entry_none
)
132 return FN(PART
,ZERO
)(space
, u
->type
);
134 return FN(PART
,ZERO
)(space
);
136 isl_space_free(space
);
137 return FN(PART
,copy
)(entry
->data
);
139 isl_space_free(space
);
143 /* Add "part" to "u".
144 * If "disjoint" is set, then "u" is not allowed to already have
145 * a part that is defined over a domain that overlaps with the domain
147 * Otherwise, compute the union sum of "part" and the part in "u"
148 * defined on the same space.
150 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
151 __isl_take PART
*part
, int disjoint
)
154 struct isl_hash_table_entry
*entry
;
159 empty
= FN(PART
,IS_ZERO
)(part
);
167 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
168 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
170 u
= FN(UNION
,cow
)(u
);
175 if (FN(UNION
,check_disjoint_domain_other
)(u
, part
) < 0)
177 entry
= FN(UNION
,find_part_entry
)(u
, part
->dim
, 1);
185 FN(UNION
,check_disjoint_domain
)(entry
->data
, part
) < 0)
187 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
188 FN(PART
,copy
)(part
));
191 empty
= FN(PART
,IS_ZERO
)(part
);
195 u
= FN(UNION
,remove_part_entry
)(u
, entry
);
206 /* Add "part" to "u", where "u" is assumed not to already have
207 * a part that is defined on the same space as "part".
209 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
210 __isl_take PART
*part
)
212 return FN(UNION
,add_part_generic
)(u
, part
, 1);
216 /* Allocate a UNION with the same type and the same size as "u" and
217 * with space "space".
219 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
220 __isl_take isl_space
*space
)
224 return FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
226 isl_space_free(space
);
230 /* Allocate a UNION with the same size as "u" and with space "space".
232 static __isl_give UNION
*FN(UNION
,alloc_same_size_on_space
)(__isl_keep UNION
*u
,
233 __isl_take isl_space
*space
)
237 return FN(UNION
,alloc
)(space
, u
->table
.n
);
239 isl_space_free(space
);
244 /* Allocate a UNION with the same space, the same type (if any) and
245 * the same size as "u".
247 static __isl_give UNION
*FN(UNION
,alloc_same_size
)(__isl_keep UNION
*u
)
249 return FN(UNION
,alloc_same_size_on_space
)(u
, FN(UNION
,get_space
)(u
));
252 /* Internal data structure for isl_union_*_transform_space.
253 * "fn' is applied to each entry in the input.
254 * "res" collects the results.
256 S(UNION
,transform_data
)
258 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
);
264 /* Apply data->fn to "part" and add the result to data->res.
266 static isl_stat
FN(UNION
,transform_entry
)(__isl_take PART
*part
, void *user
)
268 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *)user
;
270 part
= data
->fn(part
, data
->user
);
271 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
273 return isl_stat_error
;
278 /* Return a UNION living in "space" that is obtained by applying "fn"
279 * to each of the entries in "u".
281 static __isl_give UNION
*FN(UNION
,transform_space
)(__isl_take UNION
*u
,
283 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
285 S(UNION
,transform_data
) data
= { fn
, user
};
287 data
.res
= FN(UNION
,alloc_same_size_on_space
)(u
, space
);
288 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
289 &FN(UNION
,transform_entry
), &data
) < 0)
290 data
.res
= FN(UNION
,free
)(data
.res
);
295 /* Return a UNION that lives in the same space as "u" and that is obtained
296 * by applying "fn" to each of the entries in "u".
298 static __isl_give UNION
*FN(UNION
,transform
)(__isl_take UNION
*u
,
299 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
301 return FN(UNION
,transform_space
)(u
, FN(UNION
,get_space
)(u
), fn
, user
);
304 /* Apply data->fn to *part and store the result back into *part.
306 static isl_stat
FN(UNION
,transform_inplace_entry
)(void **part
, void *user
)
308 S(UNION
,transform_data
) *data
= (S(UNION
,transform_data
) *) user
;
310 *part
= data
->fn(*part
, data
->user
);
312 return isl_stat_error
;
316 /* Update "u" by applying "fn" to each entry.
317 * This operation is assumed not to change the number of entries nor
318 * the spaces of the entries.
320 * If there is only one reference to "u", then change "u" inplace.
321 * Otherwise, create a new UNION from "u" and discard the original.
323 static __isl_give UNION
*FN(UNION
,transform_inplace
)(__isl_take UNION
*u
,
324 __isl_give PART
*(*fn
)(__isl_take PART
*part
, void *user
), void *user
)
328 single_ref
= FN(UNION
,has_single_reference
)(u
);
330 return FN(UNION
,free
)(u
);
332 S(UNION
,transform_data
) data
= { fn
, user
};
333 if (FN(UNION
,foreach_inplace
)(u
,
334 &FN(UNION
,transform_inplace_entry
), &data
) < 0)
335 return FN(UNION
,free
)(u
);
338 return FN(UNION
,transform
)(u
, fn
, user
);
341 /* An isl_union_*_transform callback for use in isl_union_*_dup
342 * that simply returns "part".
344 static __isl_give PART
*FN(UNION
,copy_part
)(__isl_take PART
*part
, void *user
)
349 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
351 u
= FN(UNION
,copy
)(u
);
352 return FN(UNION
,transform
)(u
, &FN(UNION
,copy_part
), NULL
);
355 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
363 return FN(UNION
,dup
)(u
);
366 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
374 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
375 &FN(UNION
,free_u_entry
), NULL
);
376 isl_hash_table_clear(&u
->table
);
377 isl_space_free(u
->space
);
382 static __isl_give PART
*FN(UNION
,align_entry
)(__isl_take PART
*part
, void *user
)
384 isl_reordering
*exp
= user
;
386 exp
= isl_reordering_extend_space(isl_reordering_copy(exp
),
387 FN(PART
,get_domain_space
)(part
));
388 return FN(PART
,realign_domain
)(part
, exp
);
391 /* Reorder the parameters of "u" according to the given reordering.
393 static __isl_give UNION
*FN(UNION
,realign_domain
)(__isl_take UNION
*u
,
394 __isl_take isl_reordering
*r
)
401 space
= isl_space_copy(r
->dim
);
402 u
= FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,align_entry
), r
);
403 isl_reordering_free(r
);
407 isl_reordering_free(r
);
411 /* Align the parameters of "u" to those of "model".
413 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
414 __isl_take isl_space
*model
)
416 isl_bool equal_params
;
422 equal_params
= isl_space_has_equal_params(u
->space
, model
);
423 if (equal_params
< 0)
426 isl_space_free(model
);
430 model
= isl_space_params(model
);
431 r
= isl_parameter_alignment_reordering(u
->space
, model
);
432 isl_space_free(model
);
434 return FN(UNION
,realign_domain
)(u
, r
);
436 isl_space_free(model
);
441 /* Add "part" to *u, taking the union sum if "u" already has
442 * a part defined on the same space as "part".
444 static isl_stat
FN(UNION
,union_add_part
)(__isl_take PART
*part
, void *user
)
446 UNION
**u
= (UNION
**)user
;
448 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
453 /* Compute the sum of "u1" and "u2" on the union of their domains,
454 * with the actual sum on the shared domain and
455 * the defined expression on the symmetric difference of the domains.
457 * This is an internal function that is exposed under different
458 * names depending on whether the base expressions have a zero default
460 * If they do, then this function is called "add".
461 * Otherwise, it is called "union_add".
463 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
464 __isl_take UNION
*u2
)
466 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
467 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
469 u1
= FN(UNION
,cow
)(u1
);
474 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &FN(UNION
,union_add_part
), &u1
) < 0)
486 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
494 dim
= FN(PART
,get_space
)(part
);
495 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
496 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
498 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
500 u
= FN(UNION
,ZERO
)(dim
);
502 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
507 S(UNION
,match_bin_data
) {
510 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
513 /* Check if data->u2 has an element living in the same space as "part".
514 * If so, call data->fn on the two elements and add the result to
517 static isl_stat
FN(UNION
,match_bin_entry
)(__isl_take PART
*part
, void *user
)
519 S(UNION
,match_bin_data
) *data
= user
;
520 struct isl_hash_table_entry
*entry2
;
524 space
= FN(PART
,get_space
)(part
);
525 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, space
, 0);
526 isl_space_free(space
);
529 if (entry2
== isl_hash_table_entry_none
) {
534 part2
= entry2
->data
;
535 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
536 part2
->dim
, isl_dim_out
))
537 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
538 "entries should have the same range space",
541 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
543 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
545 return isl_stat_error
;
550 return isl_stat_error
;
553 /* This function is currently only used from isl_polynomial.c
554 * and not from isl_fold.c.
556 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
557 __isl_take UNION
*u2
,
558 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
559 __attribute__ ((unused
));
560 /* For each pair of elements in "u1" and "u2" living in the same space,
561 * call "fn" and collect the results.
563 static __isl_give UNION
*FN(UNION
,match_bin_op
)(__isl_take UNION
*u1
,
564 __isl_take UNION
*u2
,
565 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
567 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
569 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
570 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
576 data
.res
= FN(UNION
,alloc_same_size
)(u1
);
577 if (FN(FN(UNION
,foreach
),PARTS
)(u1
,
578 &FN(UNION
,match_bin_entry
), &data
) < 0)
587 FN(UNION
,free
)(data
.res
);
591 /* Compute the sum of "u1" and "u2".
593 * If the base expressions have a default zero value, then the sum
594 * is computed on the union of the domains of "u1" and "u2".
595 * Otherwise, it is computed on their shared domains.
597 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
600 return FN(UNION
,union_add_
)(u1
, u2
);
602 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,add
));
607 /* Subtract "u2" from "u1" and return the result.
609 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
611 return FN(UNION
,match_bin_op
)(u1
, u2
, &FN(PART
,sub
));
615 S(UNION
,any_set_data
) {
617 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
620 static __isl_give PART
*FN(UNION
,any_set_entry
)(__isl_take PART
*part
,
623 S(UNION
,any_set_data
) *data
= user
;
625 return data
->fn(part
, isl_set_copy(data
->set
));
628 /* Update each element of "u" by calling "fn" on the element and "set".
630 static __isl_give UNION
*FN(UNION
,any_set_op
)(__isl_take UNION
*u
,
631 __isl_take isl_set
*set
,
632 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
634 S(UNION
,any_set_data
) data
= { NULL
, fn
};
636 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
637 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
643 u
= FN(UNION
,transform
)(u
, &FN(UNION
,any_set_entry
), &data
);
652 /* Intersect the domain of "u" with the parameter domain "context".
654 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
655 __isl_take isl_set
*set
)
657 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,intersect_params
));
660 /* Compute the gist of the domain of "u" with respect to
661 * the parameter domain "context".
663 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
664 __isl_take isl_set
*set
)
666 return FN(UNION
,any_set_op
)(u
, set
, &FN(PW
,gist_params
));
669 S(UNION
,match_domain_data
) {
672 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
675 static int FN(UNION
,set_has_dim
)(const void *entry
, const void *val
)
677 isl_set
*set
= (isl_set
*)entry
;
678 isl_space
*dim
= (isl_space
*)val
;
680 return isl_space_is_equal(set
->dim
, dim
);
683 /* Find the set in data->uset that lives in the same space as the domain
684 * of "part", apply data->fn to *entry and this set (if any), and add
685 * the result to data->res.
687 static isl_stat
FN(UNION
,match_domain_entry
)(__isl_take PART
*part
, void *user
)
689 S(UNION
,match_domain_data
) *data
= user
;
691 struct isl_hash_table_entry
*entry2
;
694 space
= FN(PART
,get_domain_space
)(part
);
695 hash
= isl_space_get_hash(space
);
696 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
697 hash
, &FN(UNION
,set_has_dim
), space
, 0);
698 isl_space_free(space
);
704 part
= data
->fn(part
, isl_set_copy(entry2
->data
));
706 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
708 return isl_stat_error
;
713 /* Apply fn to each pair of PW in u and set in uset such that
714 * the set lives in the same space as the domain of PW
715 * and collect the results.
717 static __isl_give UNION
*FN(UNION
,match_domain_op
)(__isl_take UNION
*u
,
718 __isl_take isl_union_set
*uset
,
719 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
721 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
723 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
724 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
730 data
.res
= FN(UNION
,alloc_same_size
)(u
);
731 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
732 &FN(UNION
,match_domain_entry
), &data
) < 0)
736 isl_union_set_free(uset
);
740 isl_union_set_free(uset
);
741 FN(UNION
,free
)(data
.res
);
745 /* Intersect the domain of "u" with "uset".
746 * If "uset" is a parameters domain, then intersect the parameter
747 * domain of "u" with this set.
749 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
750 __isl_take isl_union_set
*uset
)
752 if (isl_union_set_is_params(uset
))
753 return FN(UNION
,intersect_params
)(u
,
754 isl_set_from_union_set(uset
));
755 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,intersect_domain
));
758 /* Take the set (which may be empty) in data->uset that lives
759 * in the same space as the domain of "pw", subtract it from the domain
760 * of "part" and return the result.
762 static __isl_give PART
*FN(UNION
,subtract_domain_entry
)(__isl_take PART
*part
,
765 isl_union_set
*uset
= user
;
769 space
= FN(PART
,get_domain_space
)(part
);
770 set
= isl_union_set_extract_set(uset
, space
);
771 return FN(PART
,subtract_domain
)(part
, set
);
774 /* Subtract "uset' from the domain of "u".
776 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
777 __isl_take isl_union_set
*uset
)
779 u
= FN(UNION
,transform
)(u
, &FN(UNION
,subtract_domain_entry
), uset
);
780 isl_union_set_free(uset
);
784 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
785 __isl_take isl_union_set
*uset
)
787 if (isl_union_set_is_params(uset
))
788 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
789 return FN(UNION
,match_domain_op
)(u
, uset
, &FN(PW
,gist
));
792 /* Coalesce an entry in a UNION. Coalescing is performed in-place.
793 * Since the UNION may have several references, the entry is only
794 * replaced if the coalescing is successful.
796 static isl_stat
FN(UNION
,coalesce_entry
)(void **entry
, void *user
)
798 PART
**part_p
= (PART
**) entry
;
801 part
= FN(PART
,copy
)(*part_p
);
802 part
= FN(PW
,coalesce
)(part
);
804 return isl_stat_error
;
805 FN(PART
,free
)(*part_p
);
811 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
813 if (FN(UNION
,foreach_inplace
)(u
, &FN(UNION
,coalesce_entry
), NULL
) < 0)
822 static isl_stat
FN(UNION
,domain_entry
)(__isl_take PART
*part
, void *user
)
824 isl_union_set
**uset
= (isl_union_set
**)user
;
826 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
831 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
835 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
836 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &FN(UNION
,domain_entry
), &uset
) < 0)
843 isl_union_set_free(uset
);
849 /* Negate the type of "u".
851 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
853 u
= FN(UNION
,cow
)(u
);
856 u
->type
= isl_fold_type_negate(u
->type
);
860 /* Negate the type of "u".
861 * Since "u" does not have a type, do nothing.
863 static __isl_give UNION
*FN(UNION
,negate_type
)(__isl_take UNION
*u
)
869 /* Multiply "part" by the isl_val "user" and return the result.
871 static __isl_give PART
*FN(UNION
,scale_val_entry
)(__isl_take PART
*part
,
876 return FN(PART
,scale_val
)(part
, isl_val_copy(v
));
879 /* Multiply "u" by "v" and return the result.
881 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
882 __isl_take isl_val
*v
)
886 if (isl_val_is_one(v
)) {
891 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
893 isl_space
*space
= FN(UNION
,get_space
)(u
);
895 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
897 zero
= FN(UNION
,ZERO
)(space
);
904 if (!isl_val_is_rat(v
))
905 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
906 "expecting rational factor", goto error
);
908 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,scale_val_entry
), v
);
909 if (isl_val_is_neg(v
))
910 u
= FN(UNION
,negate_type
)(u
);
920 /* Divide "part" by the isl_val "user" and return the result.
922 static __isl_give PART
*FN(UNION
,scale_down_val_entry
)(__isl_take PART
*part
,
927 return FN(PART
,scale_down_val
)(part
, isl_val_copy(v
));
930 /* Divide "u" by "v" and return the result.
932 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
933 __isl_take isl_val
*v
)
937 if (isl_val_is_one(v
)) {
942 if (!isl_val_is_rat(v
))
943 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
944 "expecting rational factor", goto error
);
945 if (isl_val_is_zero(v
))
946 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
947 "cannot scale down by zero", goto error
);
949 u
= FN(UNION
,transform_inplace
)(u
, &FN(UNION
,scale_down_val_entry
), v
);
950 if (isl_val_is_neg(v
))
951 u
= FN(UNION
,negate_type
)(u
);
961 S(UNION
,plain_is_equal_data
)
967 static isl_stat
FN(UNION
,plain_is_equal_entry
)(void **entry
, void *user
)
969 S(UNION
,plain_is_equal_data
) *data
= user
;
970 struct isl_hash_table_entry
*entry2
;
973 entry2
= FN(UNION
,find_part_entry
)(data
->u2
, pw
->dim
, 0);
974 if (!entry2
|| entry2
== isl_hash_table_entry_none
) {
976 data
->is_equal
= isl_bool_error
;
978 data
->is_equal
= isl_bool_false
;
979 return isl_stat_error
;
982 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
983 if (data
->is_equal
< 0 || !data
->is_equal
)
984 return isl_stat_error
;
989 isl_bool
FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
991 S(UNION
,plain_is_equal_data
) data
= { NULL
, isl_bool_true
};
995 return isl_bool_error
;
997 return isl_bool_true
;
998 if (u1
->table
.n
!= u2
->table
.n
)
999 return isl_bool_false
;
1000 n1
= FN(FN(UNION
,n
),PARTS
)(u1
);
1001 n2
= FN(FN(UNION
,n
),PARTS
)(u2
);
1002 if (n1
< 0 || n2
< 0)
1003 return isl_bool_error
;
1005 return isl_bool_false
;
1007 u1
= FN(UNION
,copy
)(u1
);
1008 u2
= FN(UNION
,copy
)(u2
);
1009 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1010 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1015 if (FN(UNION
,foreach_inplace
)(u1
,
1016 &FN(UNION
,plain_is_equal_entry
), &data
) < 0 &&
1023 return data
.is_equal
;
1027 return isl_bool_error
;
1030 /* Check whether the element that "entry" points to involves any NaNs and
1031 * store the result in *nan.
1032 * Abort as soon as one such element has been found.
1034 static isl_stat
FN(UNION
,involves_nan_entry
)(void **entry
, void *user
)
1036 isl_bool
*nan
= user
;
1039 *nan
= FN(PW
,involves_nan
)(pw
);
1040 if (*nan
< 0 || !nan
)
1041 return isl_stat_error
;
1046 /* Does "u" involve any NaNs?
1048 isl_bool
FN(UNION
,involves_nan
)(__isl_keep UNION
*u
)
1050 isl_bool nan
= isl_bool_false
;
1053 return isl_bool_error
;
1055 if (FN(UNION
,foreach_inplace
)(u
,
1056 &FN(UNION
,involves_nan_entry
), &nan
) < 0 &&
1058 return isl_bool_error
;
1063 /* Internal data structure for isl_union_*_drop_dims.
1064 * type, first and n are passed to isl_*_drop_dims.
1066 S(UNION
,drop_dims_data
) {
1067 enum isl_dim_type type
;
1072 /* Drop the parameters specified by "data" from "part" and return the result.
1074 static __isl_give PART
*FN(UNION
,drop_dims_entry
)(__isl_take PART
*part
,
1077 S(UNION
,drop_dims_data
) *data
= user
;
1079 return FN(PART
,drop_dims
)(part
, data
->type
, data
->first
, data
->n
);
1082 /* Drop the specified parameters from "u".
1083 * That is, type is required to be isl_dim_param.
1085 __isl_give UNION
*FN(UNION
,drop_dims
)( __isl_take UNION
*u
,
1086 enum isl_dim_type type
, unsigned first
, unsigned n
)
1089 S(UNION
,drop_dims_data
) data
= { type
, first
, n
};
1094 if (type
!= isl_dim_param
)
1095 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1096 "can only project out parameters",
1097 return FN(UNION
,free
)(u
));
1099 space
= FN(UNION
,get_space
)(u
);
1100 space
= isl_space_drop_dims(space
, type
, first
, n
);
1101 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,drop_dims_entry
),
1105 /* Internal data structure for isl_union_*_set_dim_name.
1106 * pos is the position of the parameter that needs to be renamed.
1107 * s is the new name.
1109 S(UNION
,set_dim_name_data
) {
1114 /* Change the name of the parameter at position data->pos of "part" to data->s
1115 * and return the result.
1117 static __isl_give PART
*FN(UNION
,set_dim_name_entry
)(__isl_take PART
*part
,
1120 S(UNION
,set_dim_name_data
) *data
= user
;
1122 return FN(PART
,set_dim_name
)(part
, isl_dim_param
, data
->pos
, data
->s
);
1125 /* Change the name of the parameter at position "pos" to "s".
1126 * That is, type is required to be isl_dim_param.
1128 __isl_give UNION
*FN(UNION
,set_dim_name
)(__isl_take UNION
*u
,
1129 enum isl_dim_type type
, unsigned pos
, const char *s
)
1131 S(UNION
,set_dim_name_data
) data
= { pos
, s
};
1137 if (type
!= isl_dim_param
)
1138 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
1139 "can only set parameter names",
1140 return FN(UNION
,free
)(u
));
1142 space
= FN(UNION
,get_space
)(u
);
1143 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
1144 return FN(UNION
,transform_space
)(u
, space
,
1145 &FN(UNION
,set_dim_name_entry
), &data
);
1148 /* Reset the user pointer on all identifiers of parameters and tuples
1149 * of the space of "part" and return the result.
1151 static __isl_give PART
*FN(UNION
,reset_user_entry
)(__isl_take PART
*part
,
1154 return FN(PART
,reset_user
)(part
);
1157 /* Reset the user pointer on all identifiers of parameters and tuples
1158 * of the spaces of "u".
1160 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1164 space
= FN(UNION
,get_space
)(u
);
1165 space
= isl_space_reset_user(space
);
1166 return FN(UNION
,transform_space
)(u
, space
, &FN(UNION
,reset_user_entry
),