replace PolyLib format gist test case by isl format one
[isl.git] / isl_multi_arith_templ.c
blob6765bd23cb5356921a18503eaf61ee1a3bbf2381
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 */
11 #include <isl/space.h>
12 #include <isl_val_private.h>
14 #include <isl_multi_macro.h>
16 /* Add "multi2" to "multi1" and return the result.
18 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
19 __isl_take MULTI(BASE) *multi2)
21 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
24 /* Subtract "multi2" from "multi1" and return the result.
26 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
27 __isl_take MULTI(BASE) *multi2)
29 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
32 /* Multiply the elements of "multi" by "v" and return the result.
34 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
35 __isl_take isl_val *v)
37 int i;
39 if (!multi || !v)
40 goto error;
42 if (isl_val_is_one(v)) {
43 isl_val_free(v);
44 return multi;
47 if (!isl_val_is_rat(v))
48 isl_die(isl_val_get_ctx(v), isl_error_invalid,
49 "expecting rational factor", goto error);
51 multi = FN(MULTI(BASE),cow)(multi);
52 if (!multi)
53 return NULL;
55 for (i = 0; i < multi->n; ++i) {
56 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
57 isl_val_copy(v));
58 if (!multi->u.p[i])
59 goto error;
62 isl_val_free(v);
63 return multi;
64 error:
65 isl_val_free(v);
66 return FN(MULTI(BASE),free)(multi);
69 /* Divide the elements of "multi" by "v" and return the result.
71 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
72 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
74 int i;
76 if (!multi || !v)
77 goto error;
79 if (isl_val_is_one(v)) {
80 isl_val_free(v);
81 return multi;
84 if (!isl_val_is_rat(v))
85 isl_die(isl_val_get_ctx(v), isl_error_invalid,
86 "expecting rational factor", goto error);
87 if (isl_val_is_zero(v))
88 isl_die(isl_val_get_ctx(v), isl_error_invalid,
89 "cannot scale down by zero", goto error);
91 multi = FN(MULTI(BASE),cow)(multi);
92 if (!multi)
93 return NULL;
95 for (i = 0; i < multi->n; ++i) {
96 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
97 isl_val_copy(v));
98 if (!multi->u.p[i])
99 goto error;
102 isl_val_free(v);
103 return multi;
104 error:
105 isl_val_free(v);
106 return FN(MULTI(BASE),free)(multi);
109 /* Multiply the elements of "multi" by the corresponding element of "mv"
110 * and return the result.
112 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
113 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
115 int i;
117 if (!multi || !mv)
118 goto error;
120 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
121 mv->space, isl_dim_set))
122 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
123 "spaces don't match", goto error);
125 multi = FN(MULTI(BASE),cow)(multi);
126 if (!multi)
127 goto error;
129 for (i = 0; i < multi->n; ++i) {
130 isl_val *v;
132 v = isl_multi_val_get_val(mv, i);
133 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
134 if (!multi->u.p[i])
135 goto error;
138 isl_multi_val_free(mv);
139 return multi;
140 error:
141 isl_multi_val_free(mv);
142 return FN(MULTI(BASE),free)(multi);
145 /* Divide the elements of "multi" by the corresponding element of "mv"
146 * and return the result.
148 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
149 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
151 int i;
153 if (!multi || !mv)
154 goto error;
156 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
157 mv->space, isl_dim_set))
158 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
159 "spaces don't match", goto error);
161 multi = FN(MULTI(BASE),cow)(multi);
162 if (!multi)
163 return NULL;
165 for (i = 0; i < multi->n; ++i) {
166 isl_val *v;
168 v = isl_multi_val_get_val(mv, i);
169 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
170 if (!multi->u.p[i])
171 goto error;
174 isl_multi_val_free(mv);
175 return multi;
176 error:
177 isl_multi_val_free(mv);
178 return FN(MULTI(BASE),free)(multi);
181 /* Compute the residues of the elements of "multi" modulo
182 * the corresponding element of "mv" and return the result.
184 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
185 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
187 int i;
189 if (!multi || !mv)
190 goto error;
192 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
193 mv->space, isl_dim_set))
194 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
195 "spaces don't match", goto error);
197 multi = FN(MULTI(BASE),cow)(multi);
198 if (!multi)
199 goto error;
201 for (i = 0; i < multi->n; ++i) {
202 isl_val *v;
204 v = isl_multi_val_get_val(mv, i);
205 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
206 if (!multi->u.p[i])
207 goto error;
210 isl_multi_val_free(mv);
211 return multi;
212 error:
213 isl_multi_val_free(mv);
214 return FN(MULTI(BASE),free)(multi);
217 /* Return the opposite of "multi".
219 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
221 int i;
223 multi = FN(MULTI(BASE),cow)(multi);
224 if (!multi)
225 return NULL;
227 for (i = 0; i < multi->n; ++i) {
228 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
229 if (!multi->u.p[i])
230 return FN(MULTI(BASE),free)(multi);
233 return multi;