621fe85aee7cb2a5a6e043c7470c6a94a8f86d1f
[platform/upstream/isl.git] / isl_seq.c
1 #include "isl_seq.h"
2
3 void isl_seq_clr(isl_int *p, unsigned len)
4 {
5         int i;
6         for (i = 0; i < len; ++i)
7                 isl_int_set_si(p[i], 0);
8 }
9
10 void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
11 {
12         int i;
13         for (i = 0; i < len; ++i)
14                 isl_int_neg(dst[i], src[i]);
15 }
16
17 void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
18 {
19         int i;
20         for (i = 0; i < len; ++i)
21                 isl_int_set(dst[i], src[i]);
22 }
23
24 void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
25 {
26         int i;
27         for (i = 0; i < len; ++i)
28                 isl_int_mul(dst[i], src[i], m);
29 }
30
31 void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
32 {
33         int i;
34         for (i = 0; i < len; ++i)
35                 isl_int_divexact(dst[i], src[i], m);
36 }
37
38 void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1,
39                         isl_int m2, isl_int *src2, unsigned len)
40 {
41         int i;
42         isl_int tmp;
43
44         isl_int_init(tmp);
45         for (i = 0; i < len; ++i) {
46                 isl_int_mul(tmp, m1, src1[i]);
47                 isl_int_addmul(tmp, m2, src2[i]);
48                 isl_int_set(dst[i], tmp);
49         }
50         isl_int_clear(tmp);
51 }
52
53 /*
54  * Let d = dst[pos] and s = src[pos]
55  * dst is replaced by |s| dst - sgn(s)d src
56  */
57 void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
58                   isl_int *m)
59 {
60         isl_int a;
61         isl_int b;
62
63         if (isl_int_is_zero(dst[pos]))
64                 return;
65
66         isl_int_init(a);
67         isl_int_init(b);
68
69         isl_int_gcd(a, src[pos], dst[pos]);
70         isl_int_divexact(b, dst[pos], a);
71         if (isl_int_is_pos(src[pos]))
72                 isl_int_neg(b, b);
73         isl_int_divexact(a, src[pos], a);
74         isl_int_abs(a, a);
75         isl_seq_combine(dst, a, dst, b, src, len);
76
77         if (m)
78                 isl_int_mul(*m, *m, a);
79
80         isl_int_clear(a);
81         isl_int_clear(b);
82 }
83
84 int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
85 {
86         int i;
87         for (i = 0; i < len; ++i)
88                 if (isl_int_ne(p1[i], p2[i]))
89                         return 0;
90         return 1;
91 }
92
93 int isl_seq_first_non_zero(isl_int *p, unsigned len)
94 {
95         int i;
96
97         for (i = 0; i < len; ++i)
98                 if (!isl_int_is_zero(p[i]))
99                         return i;
100         return -1;
101 }
102
103 int isl_seq_abs_min_non_zero(isl_int *p, unsigned len)
104 {
105         int i, min = isl_seq_first_non_zero(p, len);
106         if (min < 0)
107                 return -1;
108         for (i = min + 1; i < len; ++i) {
109                 if (isl_int_is_zero(p[i]))
110                         continue;
111                 if (isl_int_abs_lt(p[i], p[min]))
112                         min = i;
113         }
114         return min;
115 }
116
117 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
118 {
119         int i, min = isl_seq_abs_min_non_zero(p, len);
120
121         if (min < 0) {
122                 isl_int_set_si(*gcd, 0);
123                 return;
124         }
125         isl_int_abs(*gcd, p[min]);
126         for (i = 0; isl_int_cmp_si(*gcd, 1) > 0 && i < len; ++i) {
127                 if (i == min)
128                         continue;
129                 if (isl_int_is_zero(p[i]))
130                         continue;
131                 isl_int_gcd(*gcd, *gcd, p[i]);
132         }
133 }
134
135 u_int32_t isl_seq_hash(isl_int *p, unsigned len, unsigned bits)
136 {
137         int i;
138         u_int32_t hash = 2166136261u;
139
140         for (i = 0; i < len; ++i) {
141                 if (isl_int_is_zero(p[i]))
142                         continue;
143                 hash *= 16777619;
144                 hash ^= (i & 0xFF);
145                 hash = isl_int_hash(p[i], hash);
146         }
147         if (bits == 32)
148                 return hash;
149         if (bits >= 16)
150                 return (hash >> bits) ^ (hash & (((u_int32_t)1 << bits) - 1));
151         return ((hash >> bits) ^ hash) & (((u_int32_t)1 << bits) - 1);
152 }