pet_not: handle NaN input
[pet.git] / aff.c
blob45208e1ed77297d57802be0b8793dd8754c193b3
1 /*
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
7 * are met:
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
32 * Leiden University.
35 #include "aff.h"
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,
44 void *user)
46 isl_val **inc = (isl_val **)user;
48 if (isl_aff_is_cst(aff)) {
49 isl_val_free(*inc);
50 *inc = isl_aff_get_constant_val(aff);
53 isl_set_free(set);
54 isl_aff_free(aff);
56 return 0;
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)
65 isl_val *v;
67 if (!pa)
68 return NULL;
69 v = isl_val_nan(isl_pw_aff_get_ctx(pa));
70 if (isl_pw_aff_n_piece(pa) != 1)
71 return v;
72 if (isl_pw_aff_foreach_piece(pa, &extract_cst, &v) < 0)
73 v = isl_val_free(v);
74 return v;
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)
82 isl_pw_aff *pa;
83 pa = isl_set_indicator_function(set);
84 pa = isl_pw_aff_intersect_domain(pa, isl_set_coalesce(dom));
85 return pa;
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)
93 isl_set *cond;
94 isl_set *dom;
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)
109 isl_set *cond, *dom;
111 if (!pa)
112 return NULL;
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);
116 isl_pw_aff_free(pa);
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);
124 return pa;
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 __isl_give isl_pw_aff *pet_to_bool(__isl_take isl_pw_aff *pa)
133 isl_set *cond, *dom;
135 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
136 cond = isl_pw_aff_non_zero_set(pa);
137 pa = indicator_function(cond, dom);
139 return pa;
142 /* Return the result of applying the comparison operator "type"
143 * to "pa1" and "pa2".
145 * In particular, construct an isl_pw_aff that is equal to 1
146 * on the subset of the shared domain of "pa1" and "pa2" where
147 * the comparison holds and 0 on the other part of the shared domain.
149 __isl_give isl_pw_aff *pet_comparison(enum pet_op_type type,
150 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
152 isl_set *dom;
153 isl_set *cond;
154 isl_pw_aff *res;
156 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa1));
157 dom = isl_set_intersect(dom, isl_pw_aff_domain(isl_pw_aff_copy(pa2)));
159 switch (type) {
160 case pet_op_lt:
161 cond = isl_pw_aff_lt_set(pa1, pa2);
162 break;
163 case pet_op_le:
164 cond = isl_pw_aff_le_set(pa1, pa2);
165 break;
166 case pet_op_gt:
167 cond = isl_pw_aff_gt_set(pa1, pa2);
168 break;
169 case pet_op_ge:
170 cond = isl_pw_aff_ge_set(pa1, pa2);
171 break;
172 case pet_op_eq:
173 cond = isl_pw_aff_eq_set(pa1, pa2);
174 break;
175 case pet_op_ne:
176 cond = isl_pw_aff_ne_set(pa1, pa2);
177 break;
178 default:
179 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_internal,
180 "not a comparison operator", cond = NULL);
181 isl_pw_aff_free(pa1);
182 isl_pw_aff_free(pa2);
185 cond = isl_set_coalesce(cond);
186 res = indicator_function(cond, dom);
188 return res;
191 /* Return "lhs && rhs", with shortcut semantics.
192 * That is, if lhs is false, then the result is defined even if rhs is not.
193 * In practice, we compute lhs ? rhs : lhs.
195 static __isl_give isl_pw_aff *pw_aff_and_then(__isl_take isl_pw_aff *lhs,
196 __isl_take isl_pw_aff *rhs)
198 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), rhs, lhs);
201 /* Return "lhs || rhs", with shortcut semantics.
202 * That is, if lhs is true, then the result is defined even if rhs is not.
203 * In practice, we compute lhs ? lhs : rhs.
205 static __isl_give isl_pw_aff *pw_aff_or_else(__isl_take isl_pw_aff *lhs,
206 __isl_take isl_pw_aff *rhs)
208 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), lhs, rhs);
211 /* Return the result of applying the boolean operator "type"
212 * to "pa1" and "pa2".
214 __isl_give isl_pw_aff *pet_boolean(enum pet_op_type type,
215 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
217 isl_ctx *ctx;
219 switch (type) {
220 case pet_op_land:
221 return pw_aff_and_then(pa1, pa2);
222 case pet_op_lor:
223 return pw_aff_or_else(pa1, pa2);
224 default:
225 ctx = isl_pw_aff_get_ctx(pa1);
226 isl_pw_aff_free(pa1);
227 isl_pw_aff_free(pa2);
228 isl_die(ctx, isl_error_internal,
229 "not a boolean operator", return NULL);