3 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
4 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
5 GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
7 Copyright 1996, 1998, 2000-2004, 2008, 2010, 2011 Free Software Foundation,
10 This file is part of the GNU MP Library.
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of either:
15 * the GNU Lesser General Public License as published by the Free
16 Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
21 * the GNU General Public License as published by the Free Software
22 Foundation; either version 2 of the License, or (at your option) any
25 or both in parallel, as here.
27 The GNU MP Library is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
29 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 You should have received copies of the GNU General Public License and the
33 GNU Lesser General Public License along with the GNU MP Library. If not,
34 see https://www.gnu.org/licenses/. */
40 #ifndef JACOBI_DC_THRESHOLD
41 #define JACOBI_DC_THRESHOLD GCD_DC_THRESHOLD
44 /* Schönhage's rules:
46 * Assume r0 = r1 q1 + r2, with r0 odd, and r1 = q2 r2 + r3
50 * (r1 | r0) = s(r1, r0) (r0 | r1) = s(r1, r0) (r2, r1)
52 * where s(x,y) = (-1)^{(x-1)(y-1)/4} = (-1)^[x = y = 3 (mod 4)].
54 * If r1 is even, r2 must be odd. We have
56 * (r1 | r0) = (r1 - r0 | r0) = (-1)^(r0-1)/2 (r0 - r1 | r0)
57 * = (-1)^(r0-1)/2 s(r0, r0 - r1) (r0 | r0 - r1)
58 * = (-1)^(r0-1)/2 s(r0, r0 - r1) (r1 | r0 - r1)
60 * Now, if r1 = 0 (mod 4), then the sign factor is +1, and repeating
63 * (r1 | r0) = (r1 | r2) = (r3 | r2)
65 * On the other hand, if r1 = 2 (mod 4), the sign factor is
66 * (-1)^{(r0-1)/2}, and repeating q1 times gives the exponent
68 * (r0-1)/2 + (r0-r1-1)/2 + ... + (r0 - (q1-1) r1)/2
69 * = q1 (r0-1)/2 + q1 (q1-1)/2
71 * and we can summarize the even case as
73 * (r1 | r0) = t(r1, r0, q1) (r3 | r2)
75 * where t(x,y,q) = (-1)^{[x = 2 (mod 4)] (q(y-1)/2 + y(q-1)/2)}
77 * What about termination? The remainder sequence ends with (0|1) = 1
78 * (or (0 | r) = 0 if r != 1). What are the possible cases? If r1 is
79 * odd, r2 may be zero. If r1 is even, then r2 = r0 - q1 r1 is odd and
80 * hence non-zero. We may have r3 = r1 - q2 r2 = 0.
82 * Examples: (11|15) = - (15|11) = - (4|11)
83 * (4|11) = (4| 3) = (1| 3)
84 * (1| 3) = (3|1) = (0|1) = 1
86 * (2|7) = (2|1) = (0|1) = 1
88 * Detail: (2|7) = (2-7|7) = (-1|7)(5|7) = -(7|5) = -(2|5)
89 * (2|5) = (2-5|5) = (-1|5)(3|5) = (5|3) = (2|3)
90 * (2|3) = (2-3|3) = (-1|3)(1|3) = -(3|1) = -(2|1)
94 /* In principle, the state consists of four variables: e (one bit), a,
95 b (two bits each), d (one bit). Collected factors are (-1)^e. a and
96 b are the least significant bits of the current remainders. d
97 (denominator) is 0 if we're currently subtracting multiplies of a
98 from b, and 1 if we're subtracting b from a.
100 e is stored in the least significant bit, while a, b and d are
101 coded as only 13 distinct values in bits 1-4, according to the
102 following table. For rows not mentioning d, the value is either
103 implied, or it doesn't matter. */
110 } decode_table
[13] = {
118 /* 7 */ { 3, 3 }, /* d = 1 */
123 /* 12 */ { 3, 3 }, /* d = 0 */
125 #define JACOBI_A(bits) (decode_table[(bits)>>1].a)
126 #define JACOBI_B(bits) (decode_table[(bits)>>1].b)
127 #endif /* WANT_ASSERT */
129 const unsigned char jacobi_table
[208] = {
130 #include "jacobitab.h"
136 jacobi_hook (void *p
, mp_srcptr gp
, mp_size_t gn
,
137 mp_srcptr qp
, mp_size_t qn
, int d
)
139 unsigned *bitsp
= (unsigned *) p
;
144 if (gn
!= 1 || gp
[0] != 1)
155 *bitsp
= mpn_jacobi_update (*bitsp
, d
, qp
[0] & 3);
159 #define CHOOSE_P(n) (2*(n) / 3)
162 mpn_jacobi_n (mp_ptr ap
, mp_ptr bp
, mp_size_t n
, unsigned bits
)
165 mp_size_t matrix_scratch
;
171 ASSERT ( (ap
[n
-1] | bp
[n
-1]) > 0);
172 ASSERT ( (bp
[0] | ap
[0]) & 1);
174 /* FIXME: Check for small sizes first, before setting up temporary
176 scratch
= MPN_GCD_SUBDIV_STEP_ITCH(n
);
178 if (ABOVE_THRESHOLD (n
, GCD_DC_THRESHOLD
))
180 mp_size_t hgcd_scratch
;
181 mp_size_t update_scratch
;
182 mp_size_t p
= CHOOSE_P (n
);
183 mp_size_t dc_scratch
;
185 matrix_scratch
= MPN_HGCD_MATRIX_INIT_ITCH (n
- p
);
186 hgcd_scratch
= mpn_hgcd_itch (n
- p
);
187 update_scratch
= p
+ n
- 1;
189 dc_scratch
= matrix_scratch
+ MAX(hgcd_scratch
, update_scratch
);
190 if (dc_scratch
> scratch
)
191 scratch
= dc_scratch
;
195 tp
= TMP_ALLOC_LIMBS(scratch
);
197 while (ABOVE_THRESHOLD (n
, JACOBI_DC_THRESHOLD
))
199 struct hgcd_matrix M
;
201 mp_size_t matrix_scratch
= MPN_HGCD_MATRIX_INIT_ITCH (n
- p
);
203 mpn_hgcd_matrix_init (&M
, n
- p
, tp
);
205 nn
= mpn_hgcd_jacobi (ap
+ p
, bp
+ p
, n
- p
, &M
, &bits
,
206 tp
+ matrix_scratch
);
209 ASSERT (M
.n
<= (n
- p
- 1)/2);
210 ASSERT (M
.n
+ p
<= (p
+ n
- 1) / 2);
211 /* Temporary storage 2 (p + M->n) <= p + n - 1. */
212 n
= mpn_hgcd_matrix_adjust (&M
, p
+ nn
, ap
, bp
, p
, tp
+ matrix_scratch
);
216 /* Temporary storage n */
217 n
= mpn_gcd_subdiv_step (ap
, bp
, n
, 0, jacobi_hook
, &bits
, tp
);
221 return bits
== BITS_FAIL
? 0 : mpn_jacobi_finish (bits
);
228 struct hgcd_matrix1 M
;
229 mp_limb_t ah
, al
, bh
, bl
;
232 mask
= ap
[n
-1] | bp
[n
-1];
235 if (mask
& GMP_NUMB_HIGHBIT
)
237 ah
= ap
[n
-1]; al
= ap
[n
-2];
238 bh
= bp
[n
-1]; bl
= bp
[n
-2];
244 count_leading_zeros (shift
, mask
);
245 ah
= MPN_EXTRACT_NUMB (shift
, ap
[n
-1], ap
[n
-2]);
246 al
= MPN_EXTRACT_NUMB (shift
, ap
[n
-2], ap
[n
-3]);
247 bh
= MPN_EXTRACT_NUMB (shift
, bp
[n
-1], bp
[n
-2]);
248 bl
= MPN_EXTRACT_NUMB (shift
, bp
[n
-2], bp
[n
-3]);
251 /* Try an mpn_nhgcd2 step */
252 if (mpn_hgcd2_jacobi (ah
, al
, bh
, bl
, &M
, &bits
))
254 n
= mpn_matrix22_mul1_inverse_vector (&M
, tp
, ap
, bp
, n
);
255 MP_PTR_SWAP (ap
, tp
);
259 /* mpn_hgcd2 has failed. Then either one of a or b is very
260 small, or the difference is very small. Perform one
261 subtraction followed by one division. */
262 n
= mpn_gcd_subdiv_step (ap
, bp
, n
, 0, &jacobi_hook
, &bits
, tp
);
266 return bits
== BITS_FAIL
? 0 : mpn_jacobi_finish (bits
);
272 MP_PTR_SWAP (ap
, bp
);
284 return 1 - 2*(bits
& 1);
286 return mpn_jacobi_base (al
, bl
, bits
<< 1);
291 int res
= mpn_jacobi_2 (ap
, bp
, bits
& 1);