AX_SUBMODULE: mention default type of library to use
[isl.git] / isl_seq.c
blob7c9459ad03b759037a6a00605139af2d88480e54
1 #include "isl_seq.h"
3 void isl_seq_clr(isl_int *p, unsigned len)
5 int i;
6 for (i = 0; i < len; ++i)
7 isl_int_set_si(p[i], 0);
10 void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
12 int i;
13 for (i = 0; i < len; ++i)
14 isl_int_neg(dst[i], src[i]);
17 void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
19 int i;
20 for (i = 0; i < len; ++i)
21 isl_int_set(dst[i], src[i]);
24 void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
26 int i;
27 for (i = 0; i < len; ++i)
28 isl_int_submul(dst[i], f, src[i]);
31 void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
33 int i;
34 for (i = 0; i < len; ++i)
35 isl_int_swap_or_set(dst[i], src[i]);
38 void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
40 int i;
41 for (i = 0; i < len; ++i)
42 isl_int_mul(dst[i], src[i], m);
45 void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
47 int i;
48 for (i = 0; i < len; ++i)
49 isl_int_divexact(dst[i], src[i], m);
52 void isl_seq_cdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
54 int i;
55 for (i = 0; i < len; ++i)
56 isl_int_cdiv_q(dst[i], src[i], m);
59 void isl_seq_fdiv_q(isl_int *dst, isl_int *src, isl_int m, unsigned len)
61 int i;
62 for (i = 0; i < len; ++i)
63 isl_int_fdiv_q(dst[i], src[i], m);
66 void isl_seq_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
68 int i;
69 for (i = 0; i < len; ++i)
70 isl_int_fdiv_r(dst[i], src[i], m);
73 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
74 isl_int m2, isl_int *src2, unsigned len)
76 int i;
77 isl_int tmp;
79 isl_int_init(tmp);
80 for (i = 0; i < len; ++i) {
81 isl_int_mul(tmp, m1, src1[i]);
82 isl_int_addmul(tmp, m2, src2[i]);
83 isl_int_set(dst[i], tmp);
85 isl_int_clear(tmp);
89 * Let d = dst[pos] and s = src[pos]
90 * dst is replaced by |s| dst - sgn(s)d src
92 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
93 isl_int *m)
95 isl_int a;
96 isl_int b;
98 if (isl_int_is_zero(dst[pos]))
99 return;
101 isl_int_init(a);
102 isl_int_init(b);
104 isl_int_gcd(a, src[pos], dst[pos]);
105 isl_int_divexact(b, dst[pos], a);
106 if (isl_int_is_pos(src[pos]))
107 isl_int_neg(b, b);
108 isl_int_divexact(a, src[pos], a);
109 isl_int_abs(a, a);
110 isl_seq_combine(dst, a, dst, b, src, len);
112 if (m)
113 isl_int_mul(*m, *m, a);
115 isl_int_clear(a);
116 isl_int_clear(b);
119 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
121 int i;
122 for (i = 0; i < len; ++i)
123 if (isl_int_ne(p1[i], p2[i]))
124 return 0;
125 return 1;
128 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
130 int i;
131 int cmp;
132 for (i = 0; i < len; ++i)
133 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
134 return cmp;
135 return 0;
138 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
140 int i;
142 for (i = 0; i < len; ++i) {
143 if (isl_int_abs_ne(p1[i], p2[i]))
144 return 0;
145 if (isl_int_is_zero(p1[i]))
146 continue;
147 if (isl_int_eq(p1[i], p2[i]))
148 return 0;
150 return 1;
153 int isl_seq_first_non_zero(isl_int *p, unsigned len)
155 int i;
157 for (i = 0; i < len; ++i)
158 if (!isl_int_is_zero(p[i]))
159 return i;
160 return -1;
163 int isl_seq_last_non_zero(isl_int *p, unsigned len)
165 int i;
167 for (i = len - 1; i >= 0; --i)
168 if (!isl_int_is_zero(p[i]))
169 return i;
170 return -1;
173 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
175 int i, min = isl_seq_first_non_zero(p, len);
176 if (min < 0)
177 return -1;
178 for (i = min + 1; i < len; ++i) {
179 if (isl_int_is_zero(p[i]))
180 continue;
181 if (isl_int_abs_lt(p[i], p[min]))
182 min = i;
184 return min;
187 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
189 int i, min = isl_seq_abs_min_non_zero(p, len);
191 if (min < 0) {
192 isl_int_set_si(*gcd, 0);
193 return;
195 isl_int_abs(*gcd, p[min]);
196 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
197 if (i == min)
198 continue;
199 if (isl_int_is_zero(p[i]))
200 continue;
201 isl_int_gcd(*gcd, *gcd, p[i]);
205 void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
207 if (len == 0)
208 return;
209 isl_seq_gcd(p, len, &ctx->normalize_gcd);
210 if (!isl_int_is_zero(ctx->normalize_gcd) &&
211 !isl_int_is_one(ctx->normalize_gcd))
212 isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
215 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
217 int i;
219 if (len == 0) {
220 isl_int_set_si(*lcm, 1);
221 return;
223 isl_int_set(*lcm, p[0]);
224 for (i = 1; i < len; ++i)
225 isl_int_lcm(*lcm, *lcm, p[i]);
228 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
229 isl_int *prod)
231 int i;
232 if (len == 0) {
233 isl_int_set_si(*prod, 0);
234 return;
236 isl_int_mul(*prod, p1[0], p2[0]);
237 for (i = 1; i < len; ++i)
238 isl_int_addmul(*prod, p1[i], p2[i]);
241 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
243 int i;
244 for (i = 0; i < len; ++i) {
245 if (isl_int_is_zero(p[i]))
246 continue;
247 hash *= 16777619;
248 hash ^= (i & 0xFF);
249 hash = isl_int_hash(p[i], hash);
251 return hash;
254 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
256 uint32_t hash = isl_hash_init();
258 return isl_seq_hash(p, len, hash);
261 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
263 uint32_t hash;
265 hash = isl_seq_get_hash(p, len);
266 return isl_hash_bits(hash, bits);