add isl_seq_fdiv_r
[isl.git] / isl_seq.c
blob4ab3172967e739753fff708e352edf0cfdc6b699
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_fdiv_r(isl_int *dst, isl_int *src, isl_int m, unsigned len)
54 int i;
55 for (i = 0; i < len; ++i)
56 isl_int_fdiv_r(dst[i], src[i], m);
59 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
60 isl_int m2, isl_int *src2, unsigned len)
62 int i;
63 isl_int tmp;
65 isl_int_init(tmp);
66 for (i = 0; i < len; ++i) {
67 isl_int_mul(tmp, m1, src1[i]);
68 isl_int_addmul(tmp, m2, src2[i]);
69 isl_int_set(dst[i], tmp);
71 isl_int_clear(tmp);
75 * Let d = dst[pos] and s = src[pos]
76 * dst is replaced by |s| dst - sgn(s)d src
78 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
79 isl_int *m)
81 isl_int a;
82 isl_int b;
84 if (isl_int_is_zero(dst[pos]))
85 return;
87 isl_int_init(a);
88 isl_int_init(b);
90 isl_int_gcd(a, src[pos], dst[pos]);
91 isl_int_divexact(b, dst[pos], a);
92 if (isl_int_is_pos(src[pos]))
93 isl_int_neg(b, b);
94 isl_int_divexact(a, src[pos], a);
95 isl_int_abs(a, a);
96 isl_seq_combine(dst, a, dst, b, src, len);
98 if (m)
99 isl_int_mul(*m, *m, a);
101 isl_int_clear(a);
102 isl_int_clear(b);
105 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
107 int i;
108 for (i = 0; i < len; ++i)
109 if (isl_int_ne(p1[i], p2[i]))
110 return 0;
111 return 1;
114 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
116 int i;
117 int cmp;
118 for (i = 0; i < len; ++i)
119 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
120 return cmp;
121 return 0;
124 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
126 int i;
128 for (i = 0; i < len; ++i) {
129 if (isl_int_abs_ne(p1[i], p2[i]))
130 return 0;
131 if (isl_int_is_zero(p1[i]))
132 continue;
133 if (isl_int_eq(p1[i], p2[i]))
134 return 0;
136 return 1;
139 int isl_seq_first_non_zero(isl_int *p, unsigned len)
141 int i;
143 for (i = 0; i < len; ++i)
144 if (!isl_int_is_zero(p[i]))
145 return i;
146 return -1;
149 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
151 int i, min = isl_seq_first_non_zero(p, len);
152 if (min < 0)
153 return -1;
154 for (i = min + 1; i < len; ++i) {
155 if (isl_int_is_zero(p[i]))
156 continue;
157 if (isl_int_abs_lt(p[i], p[min]))
158 min = i;
160 return min;
163 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
165 int i, min = isl_seq_abs_min_non_zero(p, len);
167 if (min < 0) {
168 isl_int_set_si(*gcd, 0);
169 return;
171 isl_int_abs(*gcd, p[min]);
172 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
173 if (i == min)
174 continue;
175 if (isl_int_is_zero(p[i]))
176 continue;
177 isl_int_gcd(*gcd, *gcd, p[i]);
181 void isl_seq_normalize(isl_int *p, unsigned len)
183 isl_int gcd;
185 if (len == 0)
186 return;
187 isl_int_init(gcd);
188 isl_seq_gcd(p, len, &gcd);
189 if (!isl_int_is_zero(gcd))
190 isl_seq_scale_down(p, p, gcd, len);
191 isl_int_clear(gcd);
194 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
196 int i;
198 if (len == 0) {
199 isl_int_set_si(*lcm, 1);
200 return;
202 isl_int_set(*lcm, p[0]);
203 for (i = 1; i < len; ++i)
204 isl_int_lcm(*lcm, *lcm, p[i]);
207 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
208 isl_int *prod)
210 int i;
211 if (len == 0) {
212 isl_int_set_si(*prod, 0);
213 return;
215 isl_int_mul(*prod, p1[0], p2[0]);
216 for (i = 1; i < len; ++i)
217 isl_int_addmul(*prod, p1[i], p2[i]);
220 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
222 int i;
223 for (i = 0; i < len; ++i) {
224 if (isl_int_is_zero(p[i]))
225 continue;
226 hash *= 16777619;
227 hash ^= (i & 0xFF);
228 hash = isl_int_hash(p[i], hash);
230 return hash;
233 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
235 uint32_t hash = isl_hash_init();
237 return isl_seq_hash(p, len, hash);
240 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
242 uint32_t hash;
244 hash = isl_seq_get_hash(p, len);
245 return isl_hash_bits(hash, bits);