beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / hgcd_step.c
blobe58894ff3b420150525f7d38760d61671cdf6da7
1 /* hgcd_step.c.
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 2003-2005, 2008, 2011, 2012 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 for more details.
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
35 #include "gmp.h"
36 #include "gmp-impl.h"
37 #include "longlong.h"
40 static void
41 hgcd_hook (void *p, mp_srcptr gp, mp_size_t gn,
42 mp_srcptr qp, mp_size_t qn, int d)
44 ASSERT (!gp);
45 ASSERT (d >= 0);
46 ASSERT (d <= 1);
48 MPN_NORMALIZE (qp, qn);
49 if (qn > 0)
51 struct hgcd_matrix *M = (struct hgcd_matrix *) p;
52 /* NOTES: This is a bit ugly. A tp area is passed to
53 gcd_subdiv_step, which stores q at the start of that area. We
54 now use the rest. */
55 mp_ptr tp = (mp_ptr) qp + qn;
56 mpn_hgcd_matrix_update_q (M, qp, qn, d, tp);
60 /* Perform a few steps, using some of mpn_hgcd2, subtraction and
61 division. Reduces the size by almost one limb or more, but never
62 below the given size s. Return new size for a and b, or 0 if no
63 more steps are possible.
65 If hgcd2 succeeds, needs temporary space for hgcd_matrix_mul_1, M->n
66 limbs, and hgcd_mul_matrix1_inverse_vector, n limbs. If hgcd2
67 fails, needs space for the quotient, qn <= n - s limbs, for and
68 hgcd_matrix_update_q, qn + (size of the appropriate column of M) <=
69 (resulting size of M) + 1.
71 If N is the input size to the calling hgcd, then s = floor(N/2) +
72 1, M->n < N, qn + product size <= n - s + n - s + 1 = 2 (n - s) + 1
73 <= N.
76 mp_size_t
77 mpn_hgcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s,
78 struct hgcd_matrix *M, mp_ptr tp)
80 struct hgcd_matrix1 M1;
81 mp_limb_t mask;
82 mp_limb_t ah, al, bh, bl;
84 ASSERT (n > s);
86 mask = ap[n-1] | bp[n-1];
87 ASSERT (mask > 0);
89 if (n == s + 1)
91 if (mask < 4)
92 goto subtract;
94 ah = ap[n-1]; al = ap[n-2];
95 bh = bp[n-1]; bl = bp[n-2];
97 else if (mask & GMP_NUMB_HIGHBIT)
99 ah = ap[n-1]; al = ap[n-2];
100 bh = bp[n-1]; bl = bp[n-2];
102 else
104 int shift;
106 count_leading_zeros (shift, mask);
107 ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]);
108 al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]);
109 bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]);
110 bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]);
113 /* Try an mpn_hgcd2 step */
114 if (mpn_hgcd2 (ah, al, bh, bl, &M1))
116 /* Multiply M <- M * M1 */
117 mpn_hgcd_matrix_mul_1 (M, &M1, tp);
119 /* Can't swap inputs, so we need to copy. */
120 MPN_COPY (tp, ap, n);
121 /* Multiply M1^{-1} (a;b) */
122 return mpn_matrix22_mul1_inverse_vector (&M1, ap, tp, bp, n);
125 subtract:
127 return mpn_gcd_subdiv_step (ap, bp, n, s, hgcd_hook, M, tp);