extract out pet_boolean
[pet.git] / aff.c
blob9125a7338ce40889aebde992a917bc95b9e7b595
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 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
39 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
40 __isl_take isl_set *dom)
42 isl_pw_aff *pa;
43 pa = isl_set_indicator_function(set);
44 pa = isl_pw_aff_intersect_domain(pa, isl_set_coalesce(dom));
45 return pa;
48 /* Return "lhs && rhs", defined on the shared definition domain.
50 __isl_give isl_pw_aff *pet_and(__isl_take isl_pw_aff *lhs,
51 __isl_take isl_pw_aff *rhs)
53 isl_set *cond;
54 isl_set *dom;
56 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
57 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
58 cond = isl_set_intersect(isl_pw_aff_non_zero_set(lhs),
59 isl_pw_aff_non_zero_set(rhs));
60 return indicator_function(cond, dom);
63 /* Return "!pa", defined on the domain of "pa".
65 __isl_give isl_pw_aff *pet_not(__isl_take isl_pw_aff *pa)
67 isl_set *cond, *dom;
69 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
70 cond = isl_pw_aff_zero_set(pa);
71 pa = indicator_function(cond, dom);
73 return pa;
76 /* Return "!!pa", i.e., a function that is equal to 1 when "pa"
77 * is non-zero and equal to 0 when "pa" is equal to zero,
78 * on the domain of "pa".
80 __isl_give isl_pw_aff *pet_to_bool(__isl_take isl_pw_aff *pa)
82 isl_set *cond, *dom;
84 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
85 cond = isl_pw_aff_non_zero_set(pa);
86 pa = indicator_function(cond, dom);
88 return pa;
91 /* Return the result of applying the comparison operator "type"
92 * to "pa1" and "pa2".
94 * In particular, construct an isl_pw_aff that is equal to 1
95 * on the subset of the shared domain of "pa1" and "pa2" where
96 * the comparison holds and 0 on the other part of the shared domain.
98 __isl_give isl_pw_aff *pet_comparison(enum pet_op_type type,
99 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
101 isl_set *dom;
102 isl_set *cond;
103 isl_pw_aff *res;
105 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa1));
106 dom = isl_set_intersect(dom, isl_pw_aff_domain(isl_pw_aff_copy(pa2)));
108 switch (type) {
109 case pet_op_lt:
110 cond = isl_pw_aff_lt_set(pa1, pa2);
111 break;
112 case pet_op_le:
113 cond = isl_pw_aff_le_set(pa1, pa2);
114 break;
115 case pet_op_gt:
116 cond = isl_pw_aff_gt_set(pa1, pa2);
117 break;
118 case pet_op_ge:
119 cond = isl_pw_aff_ge_set(pa1, pa2);
120 break;
121 case pet_op_eq:
122 cond = isl_pw_aff_eq_set(pa1, pa2);
123 break;
124 case pet_op_ne:
125 cond = isl_pw_aff_ne_set(pa1, pa2);
126 break;
127 default:
128 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_internal,
129 "not a comparison operator", cond = NULL);
130 isl_pw_aff_free(pa1);
131 isl_pw_aff_free(pa2);
134 cond = isl_set_coalesce(cond);
135 res = indicator_function(cond, dom);
137 return res;
140 /* Return "lhs && rhs", with shortcut semantics.
141 * That is, if lhs is false, then the result is defined even if rhs is not.
142 * In practice, we compute lhs ? rhs : lhs.
144 static __isl_give isl_pw_aff *pw_aff_and_then(__isl_take isl_pw_aff *lhs,
145 __isl_take isl_pw_aff *rhs)
147 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), rhs, lhs);
150 /* Return "lhs || rhs", with shortcut semantics.
151 * That is, if lhs is true, then the result is defined even if rhs is not.
152 * In practice, we compute lhs ? lhs : rhs.
154 static __isl_give isl_pw_aff *pw_aff_or_else(__isl_take isl_pw_aff *lhs,
155 __isl_take isl_pw_aff *rhs)
157 return isl_pw_aff_cond(isl_pw_aff_copy(lhs), lhs, rhs);
160 /* Return the result of applying the boolean operator "type"
161 * to "pa1" and "pa2".
163 __isl_give isl_pw_aff *pet_boolean(enum pet_op_type type,
164 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
166 isl_ctx *ctx;
168 switch (type) {
169 case pet_op_land:
170 return pw_aff_and_then(pa1, pa2);
171 case pet_op_lor:
172 return pw_aff_or_else(pa1, pa2);
173 default:
174 ctx = isl_pw_aff_get_ctx(pa1);
175 isl_pw_aff_free(pa1);
176 isl_pw_aff_free(pa2);
177 isl_die(ctx, isl_error_internal,
178 "not a boolean operator", return NULL);