1 /* gcdext_subdiv_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, 2004, 2005, 2008 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 the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 /* Used when mpn_hgcd or mpn_hgcd2 has failed. Then either one of a or
29 b is small, or the difference is small. Perform one subtraction
30 followed by one division. If the gcd is found, stores it in gp and
31 *gn, and returns zero. Otherwise, compute the reduced a and b,
32 return the new size, and cofactors. */
34 /* Temporary storage: Needs n limbs for the quotient, at qp. tp must
35 point to an area large enough for the resulting cofactor, plus one
36 limb extra. All in all, 2N + 1 if N is a bound for both inputs and
39 mpn_gcdext_subdiv_step (mp_ptr gp, mp_size_t *gn, mp_ptr up, mp_size_t *usizep,
40 mp_ptr ap, mp_ptr bp, mp_size_t n,
41 mp_ptr u0, mp_ptr u1, mp_size_t *unp,
53 ASSERT (ap[an-1] > 0 || bp[an-1] > 0);
55 MPN_NORMALIZE (ap, an);
56 MPN_NORMALIZE (bp, bn);
62 if (UNLIKELY (an == 0))
65 MPN_COPY (gp, bp, bn);
68 MPN_NORMALIZE (u0, un);
69 MPN_COPY (up, u0, un);
71 *usizep = swapped ? un : -un;
75 else if (UNLIKELY (bn == 0))
78 MPN_COPY (gp, ap, an);
81 MPN_NORMALIZE (u1, un);
82 MPN_COPY (up, u1, un);
84 *usizep = swapped ? -un : un;
89 /* Arrange so that a > b, subtract an -= bn, and maintain
93 MPN_PTR_SWAP (ap, an, bp, bn);
100 MPN_CMP (c, ap, bp, an);
101 if (UNLIKELY (c == 0))
105 MP_PTR_SWAP (ap, bp);
106 MP_PTR_SWAP (u0, u1);
110 /* Reduce a -= b, u1 += u0 */
111 ASSERT_NOCARRY (mpn_sub (ap, ap, an, bp, bn));
112 MPN_NORMALIZE (ap, an);
115 u1[un] = mpn_add_n (u1, u1, u0, un);
118 /* Arrange so that a > b, and divide a = q b + r */
121 MPN_PTR_SWAP (ap, an, bp, bn);
122 MP_PTR_SWAP (u0, u1);
128 MPN_CMP (c, ap, bp, an);
129 if (UNLIKELY (c == 0))
133 MP_PTR_SWAP (ap, bp);
134 MP_PTR_SWAP (u0, u1);
139 /* Reduce a -= q b, u1 += q u0 */
141 mpn_tdiv_qr (qp, ap, 0, ap, an, bp, bn);
143 if (mpn_zero_p (ap, bn))
148 /* Update u1 += q u0 */
150 MPN_NORMALIZE (u0, u0n);
154 qn -= (qp[qn - 1] == 0);
157 mpn_mul (tp, qp, qn, u0, u0n);
159 mpn_mul (tp, u0, u0n, qp, qn);
163 ASSERT_NOCARRY (mpn_add (u1, tp, qn + u0n, u1, un));
165 un -= (u1[un-1] == 0);
169 u1[un] = mpn_add (u1, u1, un, tp, qn + u0n);