add isl_version
[isl.git] / isl_seq.c
blob5747b39d6f1aa240c348619ce7121475a26df2ff
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include "isl_seq.h"
12 void isl_seq_clr(isl_int *p, unsigned len)
14 int i;
15 for (i = 0; i < len; ++i)
16 isl_int_set_si(p[i], 0);
19 void isl_seq_set(isl_int *p, isl_int v, unsigned len)
21 int i;
22 for (i = 0; i < len; ++i)
23 isl_int_set(p[i], v);
26 void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
28 int i;
29 for (i = 0; i < len; ++i)
30 isl_int_neg(dst[i], src[i]);
33 void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
35 int i;
36 for (i = 0; i < len; ++i)
37 isl_int_set(dst[i], src[i]);
40 void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
42 int i;
43 for (i = 0; i < len; ++i)
44 isl_int_submul(dst[i], f, src[i]);
47 void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
49 int i;
50 for (i = 0; i < len; ++i)
51 isl_int_swap_or_set(dst[i], src[i]);
54 void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
56 int i;
57 for (i = 0; i < len; ++i)
58 isl_int_mul(dst[i], src[i], m);
61 void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
63 int i;
64 for (i = 0; i < len; ++i)
65 isl_int_divexact(dst[i], src[i], m);
68 void isl_seq_cdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
70 int i;
71 for (i = 0; i < len; ++i)
72 isl_int_cdiv_q(dst[i], src[i], m);
75 void isl_seq_fdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
77 int i;
78 for (i = 0; i < len; ++i)
79 isl_int_fdiv_q(dst[i], src[i], m);
82 void isl_seq_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
84 int i;
85 for (i = 0; i < len; ++i)
86 isl_int_fdiv_r(dst[i], src[i], m);
89 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
90 isl_int m2, isl_int *src2, unsigned len)
92 int i;
93 isl_int tmp;
95 isl_int_init(tmp);
96 for (i = 0; i < len; ++i) {
97 isl_int_mul(tmp, m1, src1[i]);
98 isl_int_addmul(tmp, m2, src2[i]);
99 isl_int_set(dst[i], tmp);
101 isl_int_clear(tmp);
105 * Let d = dst[pos] and s = src[pos]
106 * dst is replaced by |s| dst - sgn(s)d src
108 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
109 isl_int *m)
111 isl_int a;
112 isl_int b;
114 if (isl_int_is_zero(dst[pos]))
115 return;
117 isl_int_init(a);
118 isl_int_init(b);
120 isl_int_gcd(a, src[pos], dst[pos]);
121 isl_int_divexact(b, dst[pos], a);
122 if (isl_int_is_pos(src[pos]))
123 isl_int_neg(b, b);
124 isl_int_divexact(a, src[pos], a);
125 isl_int_abs(a, a);
126 isl_seq_combine(dst, a, dst, b, src, len);
128 if (m)
129 isl_int_mul(*m, *m, a);
131 isl_int_clear(a);
132 isl_int_clear(b);
135 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
137 int i;
138 for (i = 0; i < len; ++i)
139 if (isl_int_ne(p1[i], p2[i]))
140 return 0;
141 return 1;
144 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
146 int i;
147 int cmp;
148 for (i = 0; i < len; ++i)
149 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
150 return cmp;
151 return 0;
154 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
156 int i;
158 for (i = 0; i < len; ++i) {
159 if (isl_int_abs_ne(p1[i], p2[i]))
160 return 0;
161 if (isl_int_is_zero(p1[i]))
162 continue;
163 if (isl_int_eq(p1[i], p2[i]))
164 return 0;
166 return 1;
169 int isl_seq_first_non_zero(isl_int *p, unsigned len)
171 int i;
173 for (i = 0; i < len; ++i)
174 if (!isl_int_is_zero(p[i]))
175 return i;
176 return -1;
179 int isl_seq_last_non_zero(isl_int *p, unsigned len)
181 int i;
183 for (i = len - 1; i >= 0; --i)
184 if (!isl_int_is_zero(p[i]))
185 return i;
186 return -1;
189 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
191 int i, min = isl_seq_first_non_zero(p, len);
192 if (min < 0)
193 return -1;
194 for (i = min + 1; i < len; ++i) {
195 if (isl_int_is_zero(p[i]))
196 continue;
197 if (isl_int_abs_lt(p[i], p[min]))
198 min = i;
200 return min;
203 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
205 int i, min = isl_seq_abs_min_non_zero(p, len);
207 if (min < 0) {
208 isl_int_set_si(*gcd, 0);
209 return;
211 isl_int_abs(*gcd, p[min]);
212 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
213 if (i == min)
214 continue;
215 if (isl_int_is_zero(p[i]))
216 continue;
217 isl_int_gcd(*gcd, *gcd, p[i]);
221 void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
223 if (len == 0)
224 return;
225 isl_seq_gcd(p, len, &ctx->normalize_gcd);
226 if (!isl_int_is_zero(ctx->normalize_gcd) &&
227 !isl_int_is_one(ctx->normalize_gcd))
228 isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
231 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
233 int i;
235 if (len == 0) {
236 isl_int_set_si(*lcm, 1);
237 return;
239 isl_int_set(*lcm, p[0]);
240 for (i = 1; i < len; ++i)
241 isl_int_lcm(*lcm, *lcm, p[i]);
244 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
245 isl_int *prod)
247 int i;
248 if (len == 0) {
249 isl_int_set_si(*prod, 0);
250 return;
252 isl_int_mul(*prod, p1[0], p2[0]);
253 for (i = 1; i < len; ++i)
254 isl_int_addmul(*prod, p1[i], p2[i]);
257 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
259 int i;
260 for (i = 0; i < len; ++i) {
261 if (isl_int_is_zero(p[i]))
262 continue;
263 hash *= 16777619;
264 hash ^= (i & 0xFF);
265 hash = isl_int_hash(p[i], hash);
267 return hash;
270 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
272 uint32_t hash = isl_hash_init();
274 return isl_seq_hash(p, len, hash);
277 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
279 uint32_t hash;
281 hash = isl_seq_get_hash(p, len);
282 return isl_hash_bits(hash, bits);