forked from wbhart/bsdnt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn_quadratic.c
238 lines (183 loc) · 5.41 KB
/
nn_quadratic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "nn.h"
#include "nn_quadratic_arch.h"
#ifndef HAVE_ARCH_nn_mul_classical
word_t nn_mul_classical(nn_t r, nn_src_t a, len_t m1,
nn_src_t b, len_t m2)
{
len_t i;
word_t ci = 0;
ci = nn_mul1(r, a, m1, b[0]);
for (i = 1; i < m2; i++)
{
r[m1 + i - 1] = ci;
ci = nn_addmul1(r + i, a, m1, b[i]);
}
return ci;
}
#endif
#ifndef HAVE_ARCH_nn_mullow_classical
void nn_mullow_classical(nn_t ov, nn_t r, nn_src_t a, len_t m1,
nn_src_t b, len_t m2)
{
len_t i;
dword_t t = 0;
t = (dword_t) nn_mul1(r, a, m1, b[0]);
for (i = 1; i < m2; i++)
t += (dword_t) nn_addmul1(r + i, a, m1 - i, b[i]);
ov[0] = (word_t) t;
ov[1] = (word_t) (t >> WORD_BITS);
}
#endif
#ifndef HAVE_ARCH_nn_mulhigh_classical
word_t nn_mulhigh_classical(nn_t r, nn_src_t a, len_t m1,
nn_src_t b, len_t m2, nn_t ov)
{
len_t i;
word_t ci = 0;
dword_t t;
if (m2 == 1)
return ov[0]; /* overflow is one limb in this case */
/* a[m1 - 1] * b[1] + ov[0]*/
t = (dword_t) a[m1 - 1] * (dword_t) b[1] + (dword_t) ov[0];
r[0] = (word_t) t;
ci = (t >> WORD_BITS);
if (m2 > 2)
{
/* {a[m1 - 2], a[m1 - 1]} * b[2] + ov[1] */
r[1] = ci;
ci = nn_addmul1(r, a + m1 - 2, 2, b[2]);
t = (dword_t) ov[1] + (dword_t) r[1];
r[1] = (word_t) t;
ci += (word_t) (t >> WORD_BITS);
} else
ci += ov[1]; /* ov[1] cannot be more than 1 in this case */
for (i = 3; i < m2; i++)
{
r[i - 1] = ci;
ci = nn_addmul1(r, a + m1 - i, i, b[i]);
}
return ci;
}
#endif
#ifndef HAVE_ARCH_nn_muladd_classical
word_t nn_muladd_classical(nn_t r, nn_src_t a, nn_src_t b,
len_t m1, nn_src_t c, len_t m2)
{
len_t i;
word_t ci = 0;
ci = nn_muladd1(r, a, b, m1, c[0]);
for (i = 1; i < m2; i++)
{
r[m1 + i - 1] = ci;
ci = nn_addmul1(r + i, b, m1, c[i]);
}
return ci;
}
#endif
#ifndef HAVE_ARCH_nn_divrem_classical_preinv_c
void nn_divrem_classical_preinv_c(nn_t q, nn_t a, len_t m, nn_src_t d,
len_t n, preinv1_2_t inv, word_t ci)
{
dword_t t;
long i, j = m - n;
word_t q1, rem;
word_t norm = inv.norm;
word_t dinv = inv.dinv;
word_t d1 = inv.d1;
for (i = m - 1; i >= n - 1; i--, j--)
{
/* top "two words" of remaining dividend, shifted */
t = (((((dword_t) ci) << WORD_BITS) + (dword_t) a[i]) << norm);
/* check for special case, a1 == d1 which would cause overflow */
if ((t >> WORD_BITS) == d1) q1 = ~(word_t) 0;
else divrem21_preinv1(q1, rem, t, d1, dinv);
/* a -= d*q1 */
ci -= nn_submul1(a + j, d, n, q1);
/* correct if remainder has become negative */
while (ci)
{
q1--;
ci += nn_add_m(a + j, a + j, d, n);
}
q[j] = q1;
ci = a[i];
}
}
#endif
#ifndef HAVE_ARCH_nn_divapprox_classical_preinv_c
void nn_divapprox_classical_preinv_c(nn_t q, nn_t a, len_t m, nn_src_t d,
len_t n, preinv1_2_t inv, word_t ci)
{
dword_t t;
long i = m - 1, j = m - n;
word_t q1, rem;
word_t norm = inv.norm;
word_t dinv = inv.dinv;
word_t d1 = inv.d1;
len_t s = m - n; /* this many iterations to get to last quotient */
s = 2 + s + (d[n-1] <= 2*s); /* need two normalised words at that point */
if (s > i + 1) s = i + 1; /* ensure we don't do too many iterations */
for ( ; s >= n; i--, j--, s--)
{
/* top "two words" of remaining dividend, shifted */
t = (((((dword_t) ci) << WORD_BITS) + (dword_t) a[i]) << norm);
/* check for special case, a1 == d1 which would cause overflow */
if ((t >> WORD_BITS) == d1) q1 = ~(word_t) 0;
else divrem21_preinv1(q1, rem, t, d1, dinv);
/* a -= d*q1 */
ci -= nn_submul1(a + j, d, n, q1);
/* correct if remainder has become negative */
while (ci)
{
q1--;
ci += nn_add_m(a + j, a + j, d, n);
}
q[j] = q1;
ci = a[i];
}
d = d + n - s; /* make d length s */
a = a + i + 1 - s; /* make a length s + carry */
for ( ; i >= n - 1; i--, j--, s--)
{
/* top "two words" of remaining dividend, shifted */
t = (((((dword_t) ci) << WORD_BITS) + (dword_t) a[s - 1]) << norm);
/* check for special case, a1 == d1 which would cause overflow */
if ((t >> WORD_BITS) == d1) q1 = ~(word_t) 0;
else divrem21_preinv1(q1, rem, t, d1, dinv);
/* a -= d*q1 */
ci -= nn_submul1(a, d, s, q1);
/* correct if remainder has become negative */
while (ci)
{
q1--;
ci += nn_add_m(a, a, d, s);
}
q[j] = q1;
ci = a[s - 1];
d++;
}
}
#endif
#ifndef HAVE_ARCH_nn_div_hensel_preinv
void nn_div_hensel_preinv(nn_t ov, nn_t q, nn_t a, len_t m,
nn_src_t d, len_t n, hensel_preinv1_t inv)
{
long i;
dword_t t;
word_t ci, ct = 0;
for (i = 0; i < m - n; i++)
{
q[i] = a[i] * inv;
ci = nn_submul1(a + i, d, n, q[i]);
ct += nn_sub1(a + i + n, a + i + n, m - i - n, ci);
}
t = (dword_t) ct;
for ( ; i < m; i++)
{
q[i] = a[i] * inv;
t += (dword_t) nn_submul1(a + i, d, m - i, q[i]);
}
ov[0] = (word_t) t;
ov[1] = (t >> WORD_BITS);
}
#endif