extract out pet_not
[pet.git] / aff.c
blobe9c657fb50684f2d8e6d59e7d407bf6593f4ae17
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 the result of applying the comparison operator "type"
77 * to "pa1" and "pa2".
79 * In particular, construct an isl_pw_aff that is equal to 1
80 * on the subset of the shared domain of "pa1" and "pa2" where
81 * the comparison holds and 0 on the other part of the shared domain.
83 __isl_give isl_pw_aff *pet_comparison(enum pet_op_type type,
84 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2)
86 isl_set *dom;
87 isl_set *cond;
88 isl_pw_aff *res;
90 dom = isl_pw_aff_domain(isl_pw_aff_copy(pa1));
91 dom = isl_set_intersect(dom, isl_pw_aff_domain(isl_pw_aff_copy(pa2)));
93 switch (type) {
94 case pet_op_lt:
95 cond = isl_pw_aff_lt_set(pa1, pa2);
96 break;
97 case pet_op_le:
98 cond = isl_pw_aff_le_set(pa1, pa2);
99 break;
100 case pet_op_gt:
101 cond = isl_pw_aff_gt_set(pa1, pa2);
102 break;
103 case pet_op_ge:
104 cond = isl_pw_aff_ge_set(pa1, pa2);
105 break;
106 case pet_op_eq:
107 cond = isl_pw_aff_eq_set(pa1, pa2);
108 break;
109 case pet_op_ne:
110 cond = isl_pw_aff_ne_set(pa1, pa2);
111 break;
112 default:
113 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_internal,
114 "not a comparison operator", cond = NULL);
115 isl_pw_aff_free(pa1);
116 isl_pw_aff_free(pa2);
119 cond = isl_set_coalesce(cond);
120 res = indicator_function(cond, dom);
122 return res;