add isl_seq_sub
[isl.git] / isl_seq.c
blobb93012fabb41eb962092fd96e3d2dbd1bb3a3d62
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2011 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 */
11 #include <isl_ctx_private.h>
12 #include <isl_seq.h>
14 void isl_seq_clr(isl_int *p, unsigned len)
16 int i;
17 for (i = 0; i < len; ++i)
18 isl_int_set_si(p[i], 0);
21 void isl_seq_set_si(isl_int *p, int v, unsigned len)
23 int i;
24 for (i = 0; i < len; ++i)
25 isl_int_set_si(p[i], v);
28 void isl_seq_set(isl_int *p, isl_int v, unsigned len)
30 int i;
31 for (i = 0; i < len; ++i)
32 isl_int_set(p[i], v);
35 void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
37 int i;
38 for (i = 0; i < len; ++i)
39 isl_int_neg(dst[i], src[i]);
42 void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
44 int i;
45 for (i = 0; i < len; ++i)
46 isl_int_set(dst[i], src[i]);
49 /* Subtract the sequence "src" from the sequence "dst",
50 * both of length "len".
52 void isl_seq_sub(isl_int *dst, isl_int *src, unsigned len)
54 int i;
56 for (i = 0; i < len; ++i)
57 isl_int_sub(dst[i], dst[i], src[i]);
60 void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
62 int i;
63 for (i = 0; i < len; ++i)
64 isl_int_submul(dst[i], f, src[i]);
67 void isl_seq_addmul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
69 int i;
70 for (i = 0; i < len; ++i)
71 isl_int_addmul(dst[i], f, src[i]);
74 void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
76 int i;
77 for (i = 0; i < len; ++i)
78 isl_int_swap_or_set(dst[i], src[i]);
81 void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
83 int i;
84 for (i = 0; i < len; ++i)
85 isl_int_mul(dst[i], src[i], m);
88 void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
90 int i;
91 for (i = 0; i < len; ++i)
92 isl_int_divexact(dst[i], src[i], m);
95 void isl_seq_cdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
97 int i;
98 for (i = 0; i < len; ++i)
99 isl_int_cdiv_q(dst[i], src[i], m);
102 void isl_seq_fdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
104 int i;
105 for (i = 0; i < len; ++i)
106 isl_int_fdiv_q(dst[i], src[i], m);
109 void isl_seq_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
111 int i;
112 for (i = 0; i < len; ++i)
113 isl_int_fdiv_r(dst[i], src[i], m);
116 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
117 isl_int m2, isl_int *src2, unsigned len)
119 int i;
120 isl_int tmp;
122 if (dst == src1 && isl_int_is_one(m1)) {
123 if (isl_int_is_zero(m2))
124 return;
125 for (i = 0; i < len; ++i)
126 isl_int_addmul(src1[i], m2, src2[i]);
127 return;
130 isl_int_init(tmp);
131 for (i = 0; i < len; ++i) {
132 isl_int_mul(tmp, m1, src1[i]);
133 isl_int_addmul(tmp, m2, src2[i]);
134 isl_int_set(dst[i], tmp);
136 isl_int_clear(tmp);
139 /* Eliminate element "pos" from "dst" using "src".
140 * In particular, let d = dst[pos] and s = src[pos], then
141 * dst is replaced by (|s| dst - sgn(s)d src)/gcd(s,d),
142 * such that dst[pos] is zero after the elimination.
143 * If "m" is not NULL, then *m is multiplied by |s|/gcd(s,d).
144 * That is, it is multiplied by the same factor as "dst".
146 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
147 isl_int *m)
149 isl_int a;
150 isl_int b;
152 if (isl_int_is_zero(dst[pos]))
153 return;
155 isl_int_init(a);
156 isl_int_init(b);
158 isl_int_gcd(a, src[pos], dst[pos]);
159 isl_int_divexact(b, dst[pos], a);
160 if (isl_int_is_pos(src[pos]))
161 isl_int_neg(b, b);
162 isl_int_divexact(a, src[pos], a);
163 isl_int_abs(a, a);
164 isl_seq_combine(dst, a, dst, b, src, len);
166 if (m)
167 isl_int_mul(*m, *m, a);
169 isl_int_clear(a);
170 isl_int_clear(b);
173 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
175 int i;
176 for (i = 0; i < len; ++i)
177 if (isl_int_ne(p1[i], p2[i]))
178 return 0;
179 return 1;
182 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
184 int i;
185 int cmp;
186 for (i = 0; i < len; ++i)
187 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
188 return cmp;
189 return 0;
192 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
194 int i;
196 for (i = 0; i < len; ++i) {
197 if (isl_int_abs_ne(p1[i], p2[i]))
198 return 0;
199 if (isl_int_is_zero(p1[i]))
200 continue;
201 if (isl_int_eq(p1[i], p2[i]))
202 return 0;
204 return 1;
207 int isl_seq_first_non_zero(isl_int *p, unsigned len)
209 int i;
211 for (i = 0; i < len; ++i)
212 if (!isl_int_is_zero(p[i]))
213 return i;
214 return -1;
217 int isl_seq_last_non_zero(isl_int *p, unsigned len)
219 int i;
221 for (i = len - 1; i >= 0; --i)
222 if (!isl_int_is_zero(p[i]))
223 return i;
224 return -1;
227 void isl_seq_abs_max(isl_int *p, unsigned len, isl_int *max)
229 int i;
231 isl_int_set_si(*max, 0);
233 for (i = 0; i < len; ++i)
234 if (isl_int_abs_gt(p[i], *max))
235 isl_int_abs(*max, p[i]);
238 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
240 int i, min = isl_seq_first_non_zero(p, len);
241 if (min < 0)
242 return -1;
243 for (i = min + 1; i < len; ++i) {
244 if (isl_int_is_zero(p[i]))
245 continue;
246 if (isl_int_abs_lt(p[i], p[min]))
247 min = i;
249 return min;
252 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
254 int i, min = isl_seq_abs_min_non_zero(p, len);
256 if (min < 0) {
257 isl_int_set_si(*gcd, 0);
258 return;
260 isl_int_abs(*gcd, p[min]);
261 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
262 if (i == min)
263 continue;
264 if (isl_int_is_zero(p[i]))
265 continue;
266 isl_int_gcd(*gcd, *gcd, p[i]);
270 void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
272 if (len == 0)
273 return;
274 isl_seq_gcd(p, len, &ctx->normalize_gcd);
275 if (!isl_int_is_zero(ctx->normalize_gcd) &&
276 !isl_int_is_one(ctx->normalize_gcd))
277 isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
280 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
282 int i;
284 if (len == 0) {
285 isl_int_set_si(*lcm, 1);
286 return;
288 isl_int_set(*lcm, p[0]);
289 for (i = 1; i < len; ++i)
290 isl_int_lcm(*lcm, *lcm, p[i]);
293 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
294 isl_int *prod)
296 int i;
297 if (len == 0) {
298 isl_int_set_si(*prod, 0);
299 return;
301 isl_int_mul(*prod, p1[0], p2[0]);
302 for (i = 1; i < len; ++i)
303 isl_int_addmul(*prod, p1[i], p2[i]);
306 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
308 int i;
309 for (i = 0; i < len; ++i) {
310 if (isl_int_is_zero(p[i]))
311 continue;
312 hash *= 16777619;
313 hash ^= (i & 0xFF);
314 hash = isl_int_hash(p[i], hash);
316 return hash;
319 /* Given two affine expressions "p" of length p_len (including the
320 * denominator and the constant term) and "subs" of length subs_len,
321 * plug in "subs" for the variable at position "pos".
322 * The variables of "subs" and "p" are assumed to match up to subs_len,
323 * but "p" may have additional variables.
324 * "v" is an initialized isl_int that can be used internally.
326 * In particular, if "p" represents the expression
328 * (a i + g)/m
330 * with i the variable at position "pos" and "subs" represents the expression
332 * f/d
334 * then the result represents the expression
336 * (a f + d g)/(m d)
339 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
340 int p_len, int subs_len, isl_int v)
342 isl_int_set(v, p[1 + pos]);
343 isl_int_set_si(p[1 + pos], 0);
344 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
345 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
346 isl_int_mul(p[0], p[0], subs[0]);
349 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
351 uint32_t hash = isl_hash_init();
353 return isl_seq_hash(p, len, hash);
356 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
358 uint32_t hash;
360 hash = isl_seq_get_hash(p, len);
361 return isl_hash_bits(hash, bits);
364 void isl_seq_dump(isl_int *p, unsigned len)
366 int i;
368 for (i = 0; i < len; ++i) {
369 if (i)
370 fprintf(stderr, " ");
371 isl_int_print(stderr, p[i], 0);
373 fprintf(stderr, "\n");