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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/* jacobi_2.c
THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
Copyright 1996, 1998, 2000-2004, 2008, 2010 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
#include "gmp-impl.h"
#include "longlong.h"
#ifndef JACOBI_2_METHOD
#define JACOBI_2_METHOD 2
#endif
/* Computes (a / b) where b is odd, and a and b are otherwise arbitrary
two-limb numbers. */
#if JACOBI_2_METHOD == 1
int
mpn_jacobi_2 (mp_srcptr ap, mp_srcptr bp, unsigned bit)
{
mp_limb_t ah, al, bh, bl;
int c;
al = ap[0];
ah = ap[1];
bl = bp[0];
bh = bp[1];
ASSERT (bl & 1);
bl = ((bh << (GMP_NUMB_BITS - 1)) & GMP_NUMB_MASK) | (bl >> 1);
bh >>= 1;
if ( (bh | bl) == 0)
return 1 - 2*(bit & 1);
if ( (ah | al) == 0)
return 0;
if (al == 0)
{
al = ah;
ah = 0;
bit ^= GMP_NUMB_BITS & (bl ^ (bl >> 1));
}
count_trailing_zeros (c, al);
bit ^= c & (bl ^ (bl >> 1));
c++;
if (UNLIKELY (c == GMP_NUMB_BITS))
{
al = ah;
ah = 0;
}
else
{
al = ((ah << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (al >> c);
ah >>= c;
}
while ( (ah | bh) > 0)
{
mp_limb_t th, tl;
mp_limb_t bgta;
sub_ddmmss (th, tl, ah, al, bh, bl);
if ( (tl | th) == 0)
return 0;
bgta = LIMB_HIGHBIT_TO_MASK (th);
/* If b > a, invoke reciprocity */
bit ^= (bgta & al & bl);
/* b <-- min (a, b) */
add_ssaaaa (bh, bl, bh, bl, th & bgta, tl & bgta);
if ( (bh | bl) == 0)
return 1 - 2*(bit & 1);
/* a <-- |a - b| */
al = (bgta ^ tl) - bgta;
ah = (bgta ^ th);
if (UNLIKELY (al == 0))
{
/* If b > a, al == 0 implies that we have a carry to
propagate. */
al = ah - bgta;
ah = 0;
bit ^= GMP_NUMB_BITS & (bl ^ (bl >> 1));
}
count_trailing_zeros (c, al);
c++;
bit ^= c & (bl ^ (bl >> 1));
if (UNLIKELY (c == GMP_NUMB_BITS))
{
al = ah;
ah = 0;
}
else
{
al = ((ah << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (al >> c);
ah >>= c;
}
}
ASSERT (bl > 0);
while ( (al | bl) & GMP_LIMB_HIGHBIT)
{
/* Need an extra comparison to get the mask. */
mp_limb_t t = al - bl;
mp_limb_t bgta = - (bl > al);
if (t == 0)
return 0;
/* If b > a, invoke reciprocity */
bit ^= (bgta & al & bl);
/* b <-- min (a, b) */
bl += (bgta & t);
/* a <-- |a - b| */
al = (t ^ bgta) - bgta;
/* Number of trailing zeros is the same no matter if we look at
* t or a, but using t gives more parallelism. */
count_trailing_zeros (c, t);
c ++;
/* (2/b) = -1 if b = 3 or 5 mod 8 */
bit ^= c & (bl ^ (bl >> 1));
if (UNLIKELY (c == GMP_NUMB_BITS))
return 1 - 2*(bit & 1);
al >>= c;
}
/* Here we have a little impedance mismatch. Better to inline it? */
return mpn_jacobi_base (2*al+1, 2*bl+1, bit << 1);
}
#elif JACOBI_2_METHOD == 2
int
mpn_jacobi_2 (mp_srcptr ap, mp_srcptr bp, unsigned bit)
{
mp_limb_t ah, al, bh, bl;
int c;
al = ap[0];
ah = ap[1];
bl = bp[0];
bh = bp[1];
ASSERT (bl & 1);
/* Use bit 1. */
bit <<= 1;
if (bh == 0 && bl == 1)
/* (a|1) = 1 */
return 1 - (bit & 2);
if (al == 0)
{
if (ah == 0)
/* (0|b) = 0, b > 1 */
return 0;
count_trailing_zeros (c, ah);
bit ^= ((GMP_NUMB_BITS + c) << 1) & (bl ^ (bl >> 1));
al = bl;
bl = ah >> c;
if (bl == 1)
/* (1|b) = 1 */
return 1 - (bit & 2);
ah = bh;
bit ^= al & bl;
goto b_reduced;
}
if ( (al & 1) == 0)
{
count_trailing_zeros (c, al);
al = ((ah << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (al >> c);
ah >>= c;
bit ^= (c << 1) & (bl ^ (bl >> 1));
}
if (ah == 0)
{
if (bh > 0)
{
bit ^= al & bl;
MP_LIMB_T_SWAP (al, bl);
ah = bh;
goto b_reduced;
}
goto ab_reduced;
}
while (bh > 0)
{
/* Compute (a|b) */
while (ah > bh)
{
sub_ddmmss (ah, al, ah, al, bh, bl);
if (al == 0)
{
count_trailing_zeros (c, ah);
bit ^= ((GMP_NUMB_BITS + c) << 1) & (bl ^ (bl >> 1));
al = bl;
bl = ah >> c;
ah = bh;
bit ^= al & bl;
goto b_reduced;
}
count_trailing_zeros (c, al);
bit ^= (c << 1) & (bl ^ (bl >> 1));
al = ((ah << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (al >> c);
ah >>= c;
}
if (ah == bh)
goto cancel_hi;
if (ah == 0)
{
bit ^= al & bl;
MP_LIMB_T_SWAP (al, bl);
ah = bh;
break;
}
bit ^= al & bl;
/* Compute (b|a) */
while (bh > ah)
{
sub_ddmmss (bh, bl, bh, bl, ah, al);
if (bl == 0)
{
count_trailing_zeros (c, bh);
bit ^= ((GMP_NUMB_BITS + c) << 1) & (al ^ (al >> 1));
bl = bh >> c;
bit ^= al & bl;
goto b_reduced;
}
count_trailing_zeros (c, bl);
bit ^= (c << 1) & (al ^ (al >> 1));
bl = ((bh << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (bl >> c);
bh >>= c;
}
bit ^= al & bl;
/* Compute (a|b) */
if (ah == bh)
{
cancel_hi:
if (al < bl)
{
MP_LIMB_T_SWAP (al, bl);
bit ^= al & bl;
}
al -= bl;
if (al == 0)
return 0;
count_trailing_zeros (c, al);
bit ^= (c << 1) & (bl ^ (bl >> 1));
al >>= c;
if (al == 1)
return 1 - (bit & 2);
MP_LIMB_T_SWAP (al, bl);
bit ^= al & bl;
break;
}
}
b_reduced:
/* Compute (a|b), with b a single limb. */
ASSERT (bl & 1);
if (bl == 1)
/* (a|1) = 1 */
return 1 - (bit & 2);
while (ah > 0)
{
ah -= (al < bl);
al -= bl;
if (al == 0)
{
if (ah == 0)
return 0;
count_trailing_zeros (c, ah);
bit ^= ((GMP_NUMB_BITS + c) << 1) & (bl ^ (bl >> 1));
al = ah >> c;
goto ab_reduced;
}
count_trailing_zeros (c, al);
al = ((ah << (GMP_NUMB_BITS - c)) & GMP_NUMB_MASK) | (al >> c);
ah >>= c;
bit ^= (c << 1) & (bl ^ (bl >> 1));
}
ab_reduced:
ASSERT (bl & 1);
ASSERT (bl > 1);
return mpn_jacobi_base (al, bl, bit);
}
#else
#error Unsupported value for JACOBI_2_METHOD
#endif
|