2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
14 #define S(TYPE,NAME) xS(TYPE,NAME)
23 struct isl_hash_table table
;
26 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
);
28 isl_ctx
*FN(UNION
,get_ctx
)(__isl_keep UNION
*u
)
30 return u
? u
->space
->ctx
: NULL
;
33 __isl_give isl_space
*FN(UNION
,get_space
)(__isl_keep UNION
*u
)
37 return isl_space_copy(u
->space
);
40 /* Return the number of parameters of "u", where "type"
41 * is required to be set to isl_dim_param.
43 unsigned FN(UNION
,dim
)(__isl_keep UNION
*u
, enum isl_dim_type type
)
48 if (type
!= isl_dim_param
)
49 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
50 "can only reference parameters", return 0);
52 return isl_space_dim(u
->space
, type
);
55 /* Return the position of the parameter with the given name
57 * Return -1 if no such dimension can be found.
59 int FN(UNION
,find_dim_by_name
)(__isl_keep UNION
*u
, enum isl_dim_type type
,
64 return isl_space_find_dim_by_name(u
->space
, type
, name
);
68 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
,
69 enum isl_fold type
, int size
)
71 static __isl_give UNION
*FN(UNION
,alloc
)(__isl_take isl_space
*dim
, int size
)
76 dim
= isl_space_params(dim
);
80 u
= isl_calloc_type(dim
->ctx
, UNION
);
89 if (isl_hash_table_init(dim
->ctx
, &u
->table
, size
) < 0)
90 return FN(UNION
,free
)(u
);
99 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
, enum isl_fold type
)
101 return FN(UNION
,alloc
)(dim
, type
, 16);
104 __isl_give UNION
*FN(UNION
,ZERO
)(__isl_take isl_space
*dim
)
106 return FN(UNION
,alloc
)(dim
, 16);
110 __isl_give UNION
*FN(UNION
,copy
)(__isl_keep UNION
*u
)
119 /* Return the number of base expressions in "u".
121 int FN(FN(UNION
,n
),PARTS
)(__isl_keep UNION
*u
)
123 return u
? u
->table
.n
: 0;
126 S(UNION
,foreach_data
)
128 int (*fn
)(__isl_take PART
*part
, void *user
);
132 static int call_on_copy(void **entry
, void *user
)
135 S(UNION
,foreach_data
) *data
= (S(UNION
,foreach_data
) *)user
;
137 return data
->fn(FN(PART
,copy
)(part
), data
->user
);
140 int FN(FN(UNION
,foreach
),PARTS
)(__isl_keep UNION
*u
,
141 int (*fn
)(__isl_take PART
*part
, void *user
), void *user
)
143 S(UNION
,foreach_data
) data
= { fn
, user
};
148 return isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
149 &call_on_copy
, &data
);
152 /* Is the space of "entry" equal to "space"?
154 static int has_space(const void *entry
, const void *val
)
156 PART
*part
= (PART
*)entry
;
157 isl_space
*space
= (isl_space
*) val
;
159 return isl_space_is_equal(part
->dim
, space
);
162 /* This function is not currently used by isl_aff.c.
164 static int has_domain_space(const void *entry
, const void *val
)
165 __attribute__ ((unused
));
167 /* Is the domain space of "entry" equal to "space"?
169 static int has_domain_space(const void *entry
, const void *val
)
171 PART
*part
= (PART
*)entry
;
172 isl_space
*space
= (isl_space
*) val
;
174 if (isl_space_is_params(space
))
175 return isl_space_is_set(part
->dim
);
177 return isl_space_tuple_is_equal(part
->dim
, isl_dim_in
,
181 /* Is the domain space of "entry" equal to the domain of "space"?
183 static int has_same_domain_space(const void *entry
, const void *val
)
185 PART
*part
= (PART
*)entry
;
186 isl_space
*space
= (isl_space
*) val
;
188 if (isl_space_is_set(space
))
189 return isl_space_is_set(part
->dim
);
191 return isl_space_tuple_is_equal(part
->dim
, isl_dim_in
,
195 /* Extract the element of "u" living in "space" (ignoring parameters).
197 * Return the ZERO element if "u" does not contain any element
200 __isl_give PART
*FN(FN(UNION
,extract
),PARTS
)(__isl_keep UNION
*u
,
201 __isl_take isl_space
*space
)
204 struct isl_hash_table_entry
*entry
;
208 if (!isl_space_match(u
->space
, isl_dim_param
, space
, isl_dim_param
)) {
209 space
= isl_space_drop_dims(space
, isl_dim_param
,
210 0, isl_space_dim(space
, isl_dim_param
));
211 space
= isl_space_align_params(space
,
212 FN(UNION
,get_space
)(u
));
217 hash
= isl_space_get_hash(space
);
218 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
, hash
,
219 &has_space
, space
, 0);
222 return FN(PART
,ZERO
)(space
, u
->type
);
224 return FN(PART
,ZERO
)(space
);
226 isl_space_free(space
);
227 return FN(PART
,copy
)(entry
->data
);
229 isl_space_free(space
);
233 /* Add "part" to "u".
234 * If "disjoint" is set, then "u" is not allowed to already have
235 * a part that is defined on the same space as "part".
236 * Otherwise, compute the union sum of "part" and the part in "u"
237 * defined on the same space.
239 static __isl_give UNION
*FN(UNION
,add_part_generic
)(__isl_take UNION
*u
,
240 __isl_take PART
*part
, int disjoint
)
244 struct isl_hash_table_entry
*entry
;
249 empty
= FN(PART
,IS_ZERO
)(part
);
257 u
= FN(UNION
,align_params
)(u
, FN(PART
,get_space
)(part
));
258 part
= FN(PART
,align_params
)(part
, FN(UNION
,get_space
)(u
));
260 u
= FN(UNION
,cow
)(u
);
265 hash
= isl_space_get_hash(part
->dim
);
266 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
, hash
,
267 &has_same_domain_space
, part
->dim
, 1);
274 PART
*entry_part
= entry
->data
;
276 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
277 "additional part should live on separate "
278 "space", goto error
);
279 if (!isl_space_tuple_is_equal(entry_part
->dim
, isl_dim_out
,
280 part
->dim
, isl_dim_out
))
281 isl_die(FN(UNION
,get_ctx
)(u
), isl_error_invalid
,
282 "union expression can only contain a single "
283 "expression over a given domain", goto error
);
284 entry
->data
= FN(PART
,union_add_
)(entry
->data
,
285 FN(PART
,copy
)(part
));
288 empty
= FN(PART
,IS_ZERO
)(part
);
292 FN(PART
,free
)(entry
->data
);
293 isl_hash_table_remove(u
->space
->ctx
, &u
->table
, entry
);
305 /* Add "part" to "u", where "u" is assumed not to already have
306 * a part that is defined on the same space as "part".
308 __isl_give UNION
*FN(FN(UNION
,add
),PARTS
)(__isl_take UNION
*u
,
309 __isl_take PART
*part
)
311 return FN(UNION
,add_part_generic
)(u
, part
, 1);
314 static int add_part(__isl_take PART
*part
, void *user
)
316 UNION
**u
= (UNION
**)user
;
318 *u
= FN(FN(UNION
,add
),PARTS
)(*u
, part
);
323 __isl_give UNION
*FN(UNION
,dup
)(__isl_keep UNION
*u
)
331 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
), u
->type
);
333 dup
= FN(UNION
,ZERO
)(isl_space_copy(u
->space
));
335 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &add_part
, &dup
) < 0)
343 __isl_give UNION
*FN(UNION
,cow
)(__isl_take UNION
*u
)
351 return FN(UNION
,dup
)(u
);
354 static int free_u_entry(void **entry
, void *user
)
361 __isl_null UNION
*FN(UNION
,free
)(__isl_take UNION
*u
)
369 isl_hash_table_foreach(u
->space
->ctx
, &u
->table
, &free_u_entry
, NULL
);
370 isl_hash_table_clear(&u
->table
);
371 isl_space_free(u
->space
);
382 static int align_entry(__isl_take PART
*part
, void *user
)
385 S(UNION
,align
) *data
= user
;
387 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
388 FN(PART
,get_domain_space
)(part
));
390 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
,
391 FN(PART
,realign_domain
)(part
, exp
));
396 static int align_entry(__isl_take PART
*part
, void *user
)
399 S(UNION
,align
) *data
= user
;
401 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
402 FN(PART
,get_space
)(part
));
404 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
,
405 FN(PART
,realign
)(part
, exp
));
411 __isl_give UNION
*FN(UNION
,align_params
)(__isl_take UNION
*u
,
412 __isl_take isl_space
*model
)
414 S(UNION
,align
) data
= { NULL
, NULL
};
419 if (isl_space_match(u
->space
, isl_dim_param
, model
, isl_dim_param
)) {
420 isl_space_free(model
);
424 model
= isl_space_params(model
);
425 data
.exp
= isl_parameter_alignment_reordering(u
->space
, model
);
430 data
.res
= FN(UNION
,alloc
)(isl_space_copy(data
.exp
->dim
),
431 u
->type
, u
->table
.n
);
433 data
.res
= FN(UNION
,alloc
)(isl_space_copy(data
.exp
->dim
), u
->table
.n
);
435 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &align_entry
, &data
) < 0)
438 isl_reordering_free(data
.exp
);
440 isl_space_free(model
);
443 isl_reordering_free(data
.exp
);
445 FN(UNION
,free
)(data
.res
);
446 isl_space_free(model
);
450 /* Add "part" to *u, taking the union sum if "u" already has
451 * a part defined on the same space as "part".
453 static int union_add_part(__isl_take PART
*part
, void *user
)
455 UNION
**u
= (UNION
**)user
;
457 *u
= FN(UNION
,add_part_generic
)(*u
, part
, 0);
462 /* Compute the sum of "u1" and "u2" on the union of their domains,
463 * with the actual sum on the shared domain and
464 * the defined expression on the symmetric difference of the domains.
466 * This is an internal function that is exposed under different
467 * names depending on whether the base expressions have a zero default
469 * If they do, then this function is called "add".
470 * Otherwise, it is called "union_add".
472 static __isl_give UNION
*FN(UNION
,union_add_
)(__isl_take UNION
*u1
,
473 __isl_take UNION
*u2
)
475 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
476 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
478 u1
= FN(UNION
,cow
)(u1
);
483 if (FN(FN(UNION
,foreach
),PARTS
)(u2
, &union_add_part
, &u1
) < 0)
495 __isl_give UNION
*FN(FN(UNION
,from
),PARTS
)(__isl_take PART
*part
)
503 dim
= FN(PART
,get_space
)(part
);
504 dim
= isl_space_drop_dims(dim
, isl_dim_in
, 0, isl_space_dim(dim
, isl_dim_in
));
505 dim
= isl_space_drop_dims(dim
, isl_dim_out
, 0, isl_space_dim(dim
, isl_dim_out
));
507 u
= FN(UNION
,ZERO
)(dim
, part
->type
);
509 u
= FN(UNION
,ZERO
)(dim
);
511 u
= FN(FN(UNION
,add
),PARTS
)(u
, part
);
516 S(UNION
,match_bin_data
) {
519 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*);
522 /* Check if data->u2 has an element living in the same space as *entry.
523 * If so, call data->fn on the two elements and add the result to
526 static int match_bin_entry(void **entry
, void *user
)
528 S(UNION
,match_bin_data
) *data
= user
;
530 struct isl_hash_table_entry
*entry2
;
535 space
= FN(PART
,get_space
)(part
);
536 hash
= isl_space_get_hash(space
);
537 entry2
= isl_hash_table_find(data
->u2
->space
->ctx
, &data
->u2
->table
,
538 hash
, &has_same_domain_space
, space
, 0);
539 isl_space_free(space
);
543 part2
= entry2
->data
;
544 if (!isl_space_tuple_is_equal(part
->dim
, isl_dim_out
,
545 part2
->dim
, isl_dim_out
))
546 isl_die(FN(UNION
,get_ctx
)(data
->u2
), isl_error_invalid
,
547 "entries should have the same range space",
550 part
= FN(PART
, copy
)(part
);
551 part
= data
->fn(part
, FN(PART
, copy
)(entry2
->data
));
553 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, part
);
560 /* This function is currently only used from isl_polynomial.c
561 * and not from isl_fold.c.
563 static __isl_give UNION
*match_bin_op(__isl_take UNION
*u1
,
564 __isl_take UNION
*u2
,
565 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
566 __attribute__ ((unused
));
567 /* For each pair of elements in "u1" and "u2" living in the same space,
568 * call "fn" and collect the results.
570 static __isl_give UNION
*match_bin_op(__isl_take UNION
*u1
,
571 __isl_take UNION
*u2
,
572 __isl_give PART
*(*fn
)(__isl_take PART
*, __isl_take PART
*))
574 S(UNION
,match_bin_data
) data
= { NULL
, NULL
, fn
};
576 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
577 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
584 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->type
,
587 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u1
->space
), u1
->table
.n
);
589 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
590 &match_bin_entry
, &data
) < 0)
599 FN(UNION
,free
)(data
.res
);
603 /* Compute the sum of "u1" and "u2".
605 * If the base expressions have a default zero value, then the sum
606 * is computed on the union of the domains of "u1" and "u2".
607 * Otherwise, it is computed on their shared domains.
609 __isl_give UNION
*FN(UNION
,add
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
612 return FN(UNION
,union_add_
)(u1
, u2
);
614 return match_bin_op(u1
, u2
, &FN(PART
,add
));
619 /* Subtract "u2" from "u1" and return the result.
621 __isl_give UNION
*FN(UNION
,sub
)(__isl_take UNION
*u1
, __isl_take UNION
*u2
)
623 return match_bin_op(u1
, u2
, &FN(PART
,sub
));
627 S(UNION
,any_set_data
) {
630 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
633 static int any_set_entry(void **entry
, void *user
)
635 S(UNION
,any_set_data
) *data
= user
;
638 pw
= FN(PW
,copy
)(pw
);
639 pw
= data
->fn(pw
, isl_set_copy(data
->set
));
641 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
648 /* Update each element of "u" by calling "fn" on the element and "set".
650 static __isl_give UNION
*any_set_op(__isl_take UNION
*u
,
651 __isl_take isl_set
*set
,
652 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
654 S(UNION
,any_set_data
) data
= { NULL
, NULL
, fn
};
656 u
= FN(UNION
,align_params
)(u
, isl_set_get_space(set
));
657 set
= isl_set_align_params(set
, FN(UNION
,get_space
)(u
));
664 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
667 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
669 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
670 &any_set_entry
, &data
) < 0)
679 FN(UNION
,free
)(data
.res
);
683 /* Intersect the domain of "u" with the parameter domain "context".
685 __isl_give UNION
*FN(UNION
,intersect_params
)(__isl_take UNION
*u
,
686 __isl_take isl_set
*set
)
688 return any_set_op(u
, set
, &FN(PW
,intersect_params
));
691 /* Compute the gist of the domain of "u" with respect to
692 * the parameter domain "context".
694 __isl_give UNION
*FN(UNION
,gist_params
)(__isl_take UNION
*u
,
695 __isl_take isl_set
*set
)
697 return any_set_op(u
, set
, &FN(PW
,gist_params
));
700 S(UNION
,match_domain_data
) {
703 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*);
706 static int set_has_dim(const void *entry
, const void *val
)
708 isl_set
*set
= (isl_set
*)entry
;
709 isl_space
*dim
= (isl_space
*)val
;
711 return isl_space_is_equal(set
->dim
, dim
);
714 /* Find the set in data->uset that lives in the same space as the domain
715 * of *entry, apply data->fn to *entry and this set (if any), and add
716 * the result to data->res.
718 static int match_domain_entry(void **entry
, void *user
)
720 S(UNION
,match_domain_data
) *data
= user
;
722 struct isl_hash_table_entry
*entry2
;
726 space
= FN(PW
,get_domain_space
)(pw
);
727 hash
= isl_space_get_hash(space
);
728 entry2
= isl_hash_table_find(data
->uset
->dim
->ctx
, &data
->uset
->table
,
729 hash
, &set_has_dim
, space
, 0);
730 isl_space_free(space
);
734 pw
= FN(PW
,copy
)(pw
);
735 pw
= data
->fn(pw
, isl_set_copy(entry2
->data
));
737 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
744 /* Apply fn to each pair of PW in u and set in uset such that
745 * the set lives in the same space as the domain of PW
746 * and collect the results.
748 static __isl_give UNION
*match_domain_op(__isl_take UNION
*u
,
749 __isl_take isl_union_set
*uset
,
750 __isl_give PW
*(*fn
)(__isl_take PW
*, __isl_take isl_set
*))
752 S(UNION
,match_domain_data
) data
= { NULL
, NULL
, fn
};
754 u
= FN(UNION
,align_params
)(u
, isl_union_set_get_space(uset
));
755 uset
= isl_union_set_align_params(uset
, FN(UNION
,get_space
)(u
));
762 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
765 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
767 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
768 &match_domain_entry
, &data
) < 0)
772 isl_union_set_free(uset
);
776 isl_union_set_free(uset
);
777 FN(UNION
,free
)(data
.res
);
781 /* Intersect the domain of "u" with "uset".
782 * If "uset" is a parameters domain, then intersect the parameter
783 * domain of "u" with this set.
785 __isl_give UNION
*FN(UNION
,intersect_domain
)(__isl_take UNION
*u
,
786 __isl_take isl_union_set
*uset
)
788 if (isl_union_set_is_params(uset
))
789 return FN(UNION
,intersect_params
)(u
,
790 isl_set_from_union_set(uset
));
791 return match_domain_op(u
, uset
, &FN(PW
,intersect_domain
));
794 /* Internal data structure for isl_union_*_subtract_domain.
795 * uset is the set that needs to be removed from the domain.
796 * res collects the results.
798 S(UNION
,subtract_domain_data
) {
803 /* Take the set (which may be empty) in data->uset that lives
804 * in the same space as the domain of "pw", subtract it from the domain
805 * of "pw" and add the result to data->res.
807 static int FN(UNION
,subtract_domain_entry
)(__isl_take PW
*pw
, void *user
)
809 S(UNION
,subtract_domain_data
) *data
= user
;
813 space
= FN(PW
,get_domain_space
)(pw
);
814 set
= isl_union_set_extract_set(data
->uset
, space
);
815 pw
= FN(PW
,subtract_domain
)(pw
, set
);
816 data
->res
= FN(FN(UNION
,add
),PARTS
)(data
->res
, pw
);
821 /* Subtract "uset' from the domain of "u".
823 __isl_give UNION
*FN(UNION
,subtract_domain
)(__isl_take UNION
*u
,
824 __isl_take isl_union_set
*uset
)
826 S(UNION
,subtract_domain_data
) data
;
833 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->type
,
836 data
.res
= FN(UNION
,alloc
)(isl_space_copy(u
->space
), u
->table
.n
);
838 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
839 &FN(UNION
,subtract_domain_entry
), &data
) < 0)
840 data
.res
= FN(UNION
,free
)(data
.res
);
843 isl_union_set_free(uset
);
847 isl_union_set_free(uset
);
851 __isl_give UNION
*FN(UNION
,gist
)(__isl_take UNION
*u
,
852 __isl_take isl_union_set
*uset
)
854 if (isl_union_set_is_params(uset
))
855 return FN(UNION
,gist_params
)(u
, isl_set_from_union_set(uset
));
856 return match_domain_op(u
, uset
, &FN(PW
,gist
));
860 __isl_give isl_val
*FN(UNION
,eval
)(__isl_take UNION
*u
,
861 __isl_take isl_point
*pnt
)
864 struct isl_hash_table_entry
*entry
;
871 space
= isl_space_copy(pnt
->dim
);
874 hash
= isl_space_get_hash(space
);
875 entry
= isl_hash_table_find(u
->space
->ctx
, &u
->table
,
876 hash
, &has_domain_space
, space
, 0);
877 isl_space_free(space
);
879 v
= isl_val_zero(isl_point_get_ctx(pnt
));
882 v
= FN(PART
,eval
)(FN(PART
,copy
)(entry
->data
), pnt
);
893 static int coalesce_entry(void **entry
, void *user
)
895 PW
**pw
= (PW
**)entry
;
897 *pw
= FN(PW
,coalesce
)(*pw
);
904 __isl_give UNION
*FN(UNION
,coalesce
)(__isl_take UNION
*u
)
909 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
910 &coalesce_entry
, NULL
) < 0)
919 static int domain(__isl_take PART
*part
, void *user
)
921 isl_union_set
**uset
= (isl_union_set
**)user
;
923 *uset
= isl_union_set_add_set(*uset
, FN(PART
,domain
)(part
));
928 __isl_give isl_union_set
*FN(UNION
,domain
)(__isl_take UNION
*u
)
932 uset
= isl_union_set_empty(FN(UNION
,get_space
)(u
));
933 if (FN(FN(UNION
,foreach
),PARTS
)(u
, &domain
, &uset
) < 0)
940 isl_union_set_free(uset
);
945 static int mul_isl_int(void **entry
, void *user
)
947 PW
**pw
= (PW
**)entry
;
950 *pw
= FN(PW
,mul_isl_int
)(*pw
, *v
);
957 __isl_give UNION
*FN(UNION
,mul_isl_int
)(__isl_take UNION
*u
, isl_int v
)
959 if (isl_int_is_one(v
))
962 if (DEFAULT_IS_ZERO
&& u
&& isl_int_is_zero(v
)) {
964 isl_space
*dim
= FN(UNION
,get_space
)(u
);
966 zero
= FN(UNION
,ZERO
)(dim
, u
->type
);
968 zero
= FN(UNION
,ZERO
)(dim
);
974 u
= FN(UNION
,cow
)(u
);
979 if (isl_int_is_neg(v
))
980 u
->type
= isl_fold_type_negate(u
->type
);
982 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
983 &mul_isl_int
, &v
) < 0)
992 /* Multiply *entry by the isl_val "user".
994 * Return 0 on success and -1 on error.
996 static int scale_val(void **entry
, void *user
)
998 PW
**pw
= (PW
**)entry
;
1001 *pw
= FN(PW
,scale_val
)(*pw
, isl_val_copy(v
));
1008 /* Multiply "u" by "v" and return the result.
1010 __isl_give UNION
*FN(UNION
,scale_val
)(__isl_take UNION
*u
,
1011 __isl_take isl_val
*v
)
1015 if (isl_val_is_one(v
)) {
1020 if (DEFAULT_IS_ZERO
&& u
&& isl_val_is_zero(v
)) {
1022 isl_space
*space
= FN(UNION
,get_space
)(u
);
1024 zero
= FN(UNION
,ZERO
)(space
, u
->type
);
1026 zero
= FN(UNION
,ZERO
)(space
);
1033 if (!isl_val_is_rat(v
))
1034 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1035 "expecting rational factor", goto error
);
1037 u
= FN(UNION
,cow
)(u
);
1042 if (isl_val_is_neg(v
))
1043 u
->type
= isl_fold_type_negate(u
->type
);
1045 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
, &scale_val
, v
) < 0)
1056 /* Divide *entry by the isl_val "user".
1058 * Return 0 on success and -1 on error.
1060 static int FN(UNION
,scale_down_val_entry
)(void **entry
, void *user
)
1062 PW
**pw
= (PW
**)entry
;
1065 *pw
= FN(PW
,scale_down_val
)(*pw
, isl_val_copy(v
));
1072 /* Divide "u" by "v" and return the result.
1074 __isl_give UNION
*FN(UNION
,scale_down_val
)(__isl_take UNION
*u
,
1075 __isl_take isl_val
*v
)
1079 if (isl_val_is_one(v
)) {
1084 if (!isl_val_is_rat(v
))
1085 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1086 "expecting rational factor", goto error
);
1087 if (isl_val_is_zero(v
))
1088 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1089 "cannot scale down by zero", goto error
);
1091 u
= FN(UNION
,cow
)(u
);
1096 if (isl_val_is_neg(v
))
1097 u
->type
= isl_fold_type_negate(u
->type
);
1099 if (isl_hash_table_foreach(FN(UNION
,get_ctx
)(u
), &u
->table
,
1100 &FN(UNION
,scale_down_val_entry
), v
) < 0)
1111 S(UNION
,plain_is_equal_data
)
1117 static int plain_is_equal_entry(void **entry
, void *user
)
1119 S(UNION
,plain_is_equal_data
) *data
= user
;
1121 struct isl_hash_table_entry
*entry2
;
1124 hash
= isl_space_get_hash(pw
->dim
);
1125 entry2
= isl_hash_table_find(data
->u2
->space
->ctx
, &data
->u2
->table
,
1126 hash
, &has_same_domain_space
, pw
->dim
, 0);
1132 data
->is_equal
= FN(PW
,plain_is_equal
)(pw
, entry2
->data
);
1133 if (data
->is_equal
< 0 || !data
->is_equal
)
1139 int FN(UNION
,plain_is_equal
)(__isl_keep UNION
*u1
, __isl_keep UNION
*u2
)
1141 S(UNION
,plain_is_equal_data
) data
= { NULL
, 1 };
1147 if (u1
->table
.n
!= u2
->table
.n
)
1150 u1
= FN(UNION
,copy
)(u1
);
1151 u2
= FN(UNION
,copy
)(u2
);
1152 u1
= FN(UNION
,align_params
)(u1
, FN(UNION
,get_space
)(u2
));
1153 u2
= FN(UNION
,align_params
)(u2
, FN(UNION
,get_space
)(u1
));
1158 if (isl_hash_table_foreach(u1
->space
->ctx
, &u1
->table
,
1159 &plain_is_equal_entry
, &data
) < 0 &&
1166 return data
.is_equal
;
1174 /* Replace *entry by its opposite.
1176 * Return 0 on success and -1 on error.
1178 static int FN(UNION
,neg_entry
)(void **entry
, void *user
)
1180 PW
**pw
= (PW
**) entry
;
1182 *pw
= FN(PW
,neg
)(*pw
);
1184 return *pw
? 0 : -1;
1187 /* Return the opposite of "u".
1189 __isl_give UNION
*FN(UNION
,neg
)(__isl_take UNION
*u
)
1191 u
= FN(UNION
,cow
)(u
);
1195 if (isl_hash_table_foreach(u
->space
->ctx
, &u
->table
,
1196 &FN(UNION
,neg_entry
), NULL
) < 0)
1197 return FN(UNION
,free
)(u
);
1203 /* Reset the user pointer on all identifiers of parameters and tuples
1204 * of the space of "part" and add the result to *res.
1206 static int FN(UNION
,reset_user_entry
)(__isl_take PART
*part
, void *user
)
1210 part
= FN(PART
,reset_user
)(part
);
1211 *res
= FN(FN(UNION
,add
),PARTS
)(*res
, part
);
1218 /* Reset the user pointer on all identifiers of parameters and tuples
1219 * of the spaces of "u".
1221 __isl_give UNION
*FN(UNION
,reset_user
)(__isl_take UNION
*u
)
1229 space
= FN(UNION
,get_space
)(u
);
1230 space
= isl_space_reset_user(space
);
1232 res
= FN(UNION
,alloc
)(space
, u
->type
, u
->table
.n
);
1234 res
= FN(UNION
,alloc
)(space
, u
->table
.n
);
1236 if (FN(FN(UNION
,foreach
),PARTS
)(u
,
1237 &FN(UNION
,reset_user_entry
), &res
) < 0)
1238 res
= FN(UNION
,free
)(res
);