2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
37 /* If the isl_pw_aff on which isl_pw_aff_foreach_piece is called
38 * has a constant expression on its only domain, then replace
39 * the isl_val in *user by this constant.
40 * The caller is assumed to have checked that this function will
41 * be called exactly once.
43 static int extract_cst(__isl_take isl_set
*set
, __isl_take isl_aff
*aff
,
46 isl_val
**inc
= (isl_val
**)user
;
48 if (isl_aff_is_cst(aff
)) {
50 *inc
= isl_aff_get_constant_val(aff
);
59 /* If "pa" represents a constant value over a single domain,
60 * then return this constant.
61 * Otherwise return NaN.
63 __isl_give isl_val
*pet_extract_cst(__isl_keep isl_pw_aff
*pa
)
69 v
= isl_val_nan(isl_pw_aff_get_ctx(pa
));
70 if (isl_pw_aff_n_piece(pa
) != 1)
72 if (isl_pw_aff_foreach_piece(pa
, &extract_cst
, &v
) < 0)
77 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
79 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
80 __isl_take isl_set
*dom
)
83 pa
= isl_set_indicator_function(set
);
84 pa
= isl_pw_aff_intersect_domain(pa
, isl_set_coalesce(dom
));
88 /* Return "lhs && rhs", defined on the shared definition domain.
90 __isl_give isl_pw_aff
*pet_and(__isl_take isl_pw_aff
*lhs
,
91 __isl_take isl_pw_aff
*rhs
)
96 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
97 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
98 cond
= isl_set_intersect(isl_pw_aff_non_zero_set(lhs
),
99 isl_pw_aff_non_zero_set(rhs
));
100 return indicator_function(cond
, dom
);
103 /* Return "!pa", defined on the domain of "pa".
105 * If "pa" involves any NaN, then return NaN.
107 __isl_give isl_pw_aff
*pet_not(__isl_take isl_pw_aff
*pa
)
113 if (isl_pw_aff_involves_nan(pa
)) {
114 isl_space
*space
= isl_pw_aff_get_domain_space(pa
);
115 isl_local_space
*ls
= isl_local_space_from_space(space
);
117 return isl_pw_aff_nan_on_domain(ls
);
120 dom
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
121 cond
= isl_pw_aff_zero_set(pa
);
122 pa
= indicator_function(cond
, dom
);
127 /* Return "!!pa", i.e., a function that is equal to 1 when "pa"
128 * is non-zero and equal to 0 when "pa" is equal to zero,
129 * on the domain of "pa".
131 * If "pa" involves any NaN, then return NaN.
133 __isl_give isl_pw_aff
*pet_to_bool(__isl_take isl_pw_aff
*pa
)
139 if (isl_pw_aff_involves_nan(pa
)) {
140 isl_space
*space
= isl_pw_aff_get_domain_space(pa
);
141 isl_local_space
*ls
= isl_local_space_from_space(space
);
143 return isl_pw_aff_nan_on_domain(ls
);
146 dom
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
147 cond
= isl_pw_aff_non_zero_set(pa
);
148 pa
= indicator_function(cond
, dom
);
153 /* Return the result of applying the comparison operator "type"
154 * to "pa1" and "pa2".
156 * In particular, construct an isl_pw_aff that is equal to 1
157 * on the subset of the shared domain of "pa1" and "pa2" where
158 * the comparison holds and 0 on the other part of the shared domain.
160 * If "pa1" or "pa2" involve any NaN, then return NaN.
162 __isl_give isl_pw_aff
*pet_comparison(enum pet_op_type type
,
163 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
)
171 if (isl_pw_aff_involves_nan(pa1
) || isl_pw_aff_involves_nan(pa2
)) {
172 isl_space
*space
= isl_pw_aff_get_domain_space(pa1
);
173 isl_local_space
*ls
= isl_local_space_from_space(space
);
174 isl_pw_aff_free(pa1
);
175 isl_pw_aff_free(pa2
);
176 return isl_pw_aff_nan_on_domain(ls
);
179 dom
= isl_pw_aff_domain(isl_pw_aff_copy(pa1
));
180 dom
= isl_set_intersect(dom
, isl_pw_aff_domain(isl_pw_aff_copy(pa2
)));
184 cond
= isl_pw_aff_lt_set(pa1
, pa2
);
187 cond
= isl_pw_aff_le_set(pa1
, pa2
);
190 cond
= isl_pw_aff_gt_set(pa1
, pa2
);
193 cond
= isl_pw_aff_ge_set(pa1
, pa2
);
196 cond
= isl_pw_aff_eq_set(pa1
, pa2
);
199 cond
= isl_pw_aff_ne_set(pa1
, pa2
);
202 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_internal
,
203 "not a comparison operator", cond
= NULL
);
204 isl_pw_aff_free(pa1
);
205 isl_pw_aff_free(pa2
);
208 cond
= isl_set_coalesce(cond
);
209 res
= indicator_function(cond
, dom
);
213 isl_pw_aff_free(pa1
);
214 isl_pw_aff_free(pa2
);
218 /* Return "lhs && rhs", with shortcut semantics.
219 * That is, if lhs is false, then the result is defined even if rhs is not.
220 * In practice, we compute lhs ? rhs : lhs.
222 static __isl_give isl_pw_aff
*pw_aff_and_then(__isl_take isl_pw_aff
*lhs
,
223 __isl_take isl_pw_aff
*rhs
)
225 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), rhs
, lhs
);
228 /* Return "lhs || rhs", with shortcut semantics.
229 * That is, if lhs is true, then the result is defined even if rhs is not.
230 * In practice, we compute lhs ? lhs : rhs.
232 static __isl_give isl_pw_aff
*pw_aff_or_else(__isl_take isl_pw_aff
*lhs
,
233 __isl_take isl_pw_aff
*rhs
)
235 return isl_pw_aff_cond(isl_pw_aff_copy(lhs
), lhs
, rhs
);
238 /* Return the result of applying the boolean operator "type"
239 * to "pa1" and "pa2".
241 __isl_give isl_pw_aff
*pet_boolean(enum pet_op_type type
,
242 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
)
248 return pw_aff_and_then(pa1
, pa2
);
250 return pw_aff_or_else(pa1
, pa2
);
252 ctx
= isl_pw_aff_get_ctx(pa1
);
253 isl_pw_aff_free(pa1
);
254 isl_pw_aff_free(pa2
);
255 isl_die(ctx
, isl_error_internal
,
256 "not a boolean operator", return NULL
);