rename F_ and FL_ macros to ISL_F_ and ISL_FL_ to avoid name clashes
[isl.git] / isl_seq.c
blob62d8da52aa12afda2a730d5e152f213d19a9e821
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_combine(isl_int *dst, isl_int m1, isl_int *src1,
53 isl_int m2, isl_int *src2, unsigned len)
55 int i;
56 isl_int tmp;
58 isl_int_init(tmp);
59 for (i = 0; i < len; ++i) {
60 isl_int_mul(tmp, m1, src1[i]);
61 isl_int_addmul(tmp, m2, src2[i]);
62 isl_int_set(dst[i], tmp);
64 isl_int_clear(tmp);
68 * Let d = dst[pos] and s = src[pos]
69 * dst is replaced by |s| dst - sgn(s)d src
71 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
72 isl_int *m)
74 isl_int a;
75 isl_int b;
77 if (isl_int_is_zero(dst[pos]))
78 return;
80 isl_int_init(a);
81 isl_int_init(b);
83 isl_int_gcd(a, src[pos], dst[pos]);
84 isl_int_divexact(b, dst[pos], a);
85 if (isl_int_is_pos(src[pos]))
86 isl_int_neg(b, b);
87 isl_int_divexact(a, src[pos], a);
88 isl_int_abs(a, a);
89 isl_seq_combine(dst, a, dst, b, src, len);
91 if (m)
92 isl_int_mul(*m, *m, a);
94 isl_int_clear(a);
95 isl_int_clear(b);
98 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
100 int i;
101 for (i = 0; i < len; ++i)
102 if (isl_int_ne(p1[i], p2[i]))
103 return 0;
104 return 1;
107 int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
109 int i;
110 int cmp;
111 for (i = 0; i < len; ++i)
112 if ((cmp = isl_int_cmp(p1[i], p2[i])) != 0)
113 return cmp;
114 return 0;
117 int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
119 int i;
121 for (i = 0; i < len; ++i) {
122 if (isl_int_abs_ne(p1[i], p2[i]))
123 return 0;
124 if (isl_int_is_zero(p1[i]))
125 continue;
126 if (isl_int_eq(p1[i], p2[i]))
127 return 0;
129 return 1;
132 int isl_seq_first_non_zero(isl_int *p, unsigned len)
134 int i;
136 for (i = 0; i < len; ++i)
137 if (!isl_int_is_zero(p[i]))
138 return i;
139 return -1;
142 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
144 int i, min = isl_seq_first_non_zero(p, len);
145 if (min < 0)
146 return -1;
147 for (i = min + 1; i < len; ++i) {
148 if (isl_int_is_zero(p[i]))
149 continue;
150 if (isl_int_abs_lt(p[i], p[min]))
151 min = i;
153 return min;
156 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
158 int i, min = isl_seq_abs_min_non_zero(p, len);
160 if (min < 0) {
161 isl_int_set_si(*gcd, 0);
162 return;
164 isl_int_abs(*gcd, p[min]);
165 for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
166 if (i == min)
167 continue;
168 if (isl_int_is_zero(p[i]))
169 continue;
170 isl_int_gcd(*gcd, *gcd, p[i]);
174 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
176 int i;
178 if (len == 0) {
179 isl_int_set_si(*lcm, 1);
180 return;
182 isl_int_set(*lcm, p[0]);
183 for (i = 1; i < len; ++i)
184 isl_int_lcm(*lcm, *lcm, p[i]);
187 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
188 isl_int *prod)
190 int i;
191 if (len == 0) {
192 isl_int_set_si(*prod, 0);
193 return;
195 isl_int_mul(*prod, p1[0], p2[0]);
196 for (i = 1; i < len; ++i)
197 isl_int_addmul(*prod, p1[i], p2[i]);
200 uint32_t isl_seq_hash(isl_int *p, unsigned len, uint32_t hash)
202 int i;
203 for (i = 0; i < len; ++i) {
204 if (isl_int_is_zero(p[i]))
205 continue;
206 hash *= 16777619;
207 hash ^= (i & 0xFF);
208 hash = isl_int_hash(p[i], hash);
210 return hash;
213 uint32_t isl_seq_get_hash(isl_int *p, unsigned len)
215 uint32_t hash = isl_hash_init();
217 return isl_seq_hash(p, len, hash);
220 uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
222 uint32_t hash;
224 hash = isl_seq_get_hash(p, len);
225 return isl_hash_bits(hash, bits);