isl_map_simplify.c: uset_gist_full: take constraint matrix as input
[isl.git] / isl_seq.c
blob2159464e02835b128c584f6a3b32fd519e86a18a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_seq.h>
13 void isl_seq_clr(isl_int *p, unsigned len)
15 int i;
16 for (i = 0; i < len; ++i)
17 isl_int_set_si(p[i], 0);
20 void isl_seq_set_si(isl_int *p, int v, unsigned len)
22 int i;
23 for (i = 0; i < len; ++i)
24 isl_int_set_si(p[i], v);
27 void isl_seq_set(isl_int *p, isl_int v, unsigned len)
29 int i;
30 for (i = 0; i < len; ++i)
31 isl_int_set(p[i], v);
34 void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
36 int i;
37 for (i = 0; i < len; ++i)
38 isl_int_neg(dst[i], src[i]);
41 void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
43 int i;
44 for (i = 0; i < len; ++i)
45 isl_int_set(dst[i], src[i]);
48 void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
50 int i;
51 for (i = 0; i < len; ++i)
52 isl_int_submul(dst[i], f, src[i]);
55 void isl_seq_addmul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
57 int i;
58 for (i = 0; i < len; ++i)
59 isl_int_addmul(dst[i], f, src[i]);
62 void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
64 int i;
65 for (i = 0; i < len; ++i)
66 isl_int_swap_or_set(dst[i], src[i]);
69 void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
71 int i;
72 for (i = 0; i < len; ++i)
73 isl_int_mul(dst[i], src[i], m);
76 void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
78 int i;
79 for (i = 0; i < len; ++i)
80 isl_int_divexact(dst[i], src[i], m);
83 void isl_seq_cdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
85 int i;
86 for (i = 0; i < len; ++i)
87 isl_int_cdiv_q(dst[i], src[i], m);
90 void isl_seq_fdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
92 int i;
93 for (i = 0; i < len; ++i)
94 isl_int_fdiv_q(dst[i], src[i], m);
97 void isl_seq_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
99 int i;
100 for (i = 0; i < len; ++i)
101 isl_int_fdiv_r(dst[i], src[i], m);
104 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
105 isl_int m2, isl_int *src2, unsigned len)
107 int i;
108 isl_int tmp;
110 if (dst == src1 && isl_int_is_one(m1)) {
111 if (isl_int_is_zero(m2))
112 return;
113 for (i = 0; i < len; ++i)
114 isl_int_addmul(src1[i], m2, src2[i]);
115 return;
118 isl_int_init(tmp);
119 for (i = 0; i < len; ++i) {
120 isl_int_mul(tmp, m1, src1[i]);
121 isl_int_addmul(tmp, m2, src2[i]);
122 isl_int_set(dst[i], tmp);
124 isl_int_clear(tmp);
128 * Let d = dst[pos] and s = src[pos]
129 * dst is replaced by |s| dst - sgn(s)d src
131 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
132 isl_int *m)
134 isl_int a;
135 isl_int b;
137 if (isl_int_is_zero(dst[pos]))
138 return;
140 isl_int_init(a);
141 isl_int_init(b);
143 isl_int_gcd(a, src[pos], dst[pos]);
144 isl_int_divexact(b, dst[pos], a);
145 if (isl_int_is_pos(src[pos]))
146 isl_int_neg(b, b);
147 isl_int_divexact(a, src[pos], a);
148 isl_int_abs(a, a);
149 isl_seq_combine(dst, a, dst, b, src, len);
151 if (m)
152 isl_int_mul(*m, *m, a);
154 isl_int_clear(a);
155 isl_int_clear(b);
158 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
160 int i;
161 for (i = 0; i < len; ++i)
162 if (isl_int_ne(p1[i], p2[i]))
163 return 0;
164 return 1;
167 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
169 int i;
170 int cmp;
171 for (i = 0; i < len; ++i)
172 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
173 return cmp;
174 return 0;
177 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
179 int i;
181 for (i = 0; i < len; ++i) {
182 if (isl_int_abs_ne(p1[i], p2[i]))
183 return 0;
184 if (isl_int_is_zero(p1[i]))
185 continue;
186 if (isl_int_eq(p1[i], p2[i]))
187 return 0;
189 return 1;
192 int isl_seq_first_non_zero(isl_int *p, unsigned len)
194 int i;
196 for (i = 0; i < len; ++i)
197 if (!isl_int_is_zero(p[i]))
198 return i;
199 return -1;
202 int isl_seq_last_non_zero(isl_int *p, unsigned len)
204 int i;
206 for (i = len - 1; i >= 0; --i)
207 if (!isl_int_is_zero(p[i]))
208 return i;
209 return -1;
212 void isl_seq_abs_max(isl_int *p, unsigned len, isl_int *max)
214 int i;
216 isl_int_set_si(*max, 0);
218 for (i = 0; i < len; ++i)
219 if (isl_int_abs_gt(p[i], *max))
220 isl_int_abs(*max, p[i]);
223 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
225 int i, min = isl_seq_first_non_zero(p, len);
226 if (min < 0)
227 return -1;
228 for (i = min + 1; i < len; ++i) {
229 if (isl_int_is_zero(p[i]))
230 continue;
231 if (isl_int_abs_lt(p[i], p[min]))
232 min = i;
234 return min;
237 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
239 int i, min = isl_seq_abs_min_non_zero(p, len);
241 if (min < 0) {
242 isl_int_set_si(*gcd, 0);
243 return;
245 isl_int_abs(*gcd, p[min]);
246 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
247 if (i == min)
248 continue;
249 if (isl_int_is_zero(p[i]))
250 continue;
251 isl_int_gcd(*gcd, *gcd, p[i]);
255 void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
257 if (len == 0)
258 return;
259 isl_seq_gcd(p, len, &ctx->normalize_gcd);
260 if (!isl_int_is_zero(ctx->normalize_gcd) &&
261 !isl_int_is_one(ctx->normalize_gcd))
262 isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
265 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
267 int i;
269 if (len == 0) {
270 isl_int_set_si(*lcm, 1);
271 return;
273 isl_int_set(*lcm, p[0]);
274 for (i = 1; i < len; ++i)
275 isl_int_lcm(*lcm, *lcm, p[i]);
278 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
279 isl_int *prod)
281 int i;
282 if (len == 0) {
283 isl_int_set_si(*prod, 0);
284 return;
286 isl_int_mul(*prod, p1[0], p2[0]);
287 for (i = 1; i < len; ++i)
288 isl_int_addmul(*prod, p1[i], p2[i]);
291 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
293 int i;
294 for (i = 0; i < len; ++i) {
295 if (isl_int_is_zero(p[i]))
296 continue;
297 hash *= 16777619;
298 hash ^= (i & 0xFF);
299 hash = isl_int_hash(p[i], hash);
301 return hash;
304 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
306 uint32_t hash = isl_hash_init();
308 return isl_seq_hash(p, len, hash);
311 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
313 uint32_t hash;
315 hash = isl_seq_get_hash(p, len);
316 return isl_hash_bits(hash, bits);
319 void isl_seq_dump(isl_int *p, unsigned len)
321 int i;
323 for (i = 0; i < len; ++i) {
324 if (i)
325 fprintf(stderr, " ");
326 isl_int_print(stderr, p[i], 0);
328 fprintf(stderr, "\n");