186d7f5d3SJohn Marino /* gcd_subdiv_step.c.
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
486d7f5d3SJohn Marino SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
586d7f5d3SJohn Marino GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
686d7f5d3SJohn Marino
786d7f5d3SJohn Marino Copyright 2003, 2004, 2005, 2008 Free Software Foundation, Inc.
886d7f5d3SJohn Marino
986d7f5d3SJohn Marino This file is part of the GNU MP Library.
1086d7f5d3SJohn Marino
1186d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1286d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1386d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1486d7f5d3SJohn Marino option) any later version.
1586d7f5d3SJohn Marino
1686d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1786d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1886d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1986d7f5d3SJohn Marino License for more details.
2086d7f5d3SJohn Marino
2186d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2286d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino #include "gmp.h"
2586d7f5d3SJohn Marino #include "gmp-impl.h"
2686d7f5d3SJohn Marino #include "longlong.h"
2786d7f5d3SJohn Marino
2886d7f5d3SJohn Marino /* Used when mpn_hgcd or mpn_hgcd2 has failed. Then either one of a or
2986d7f5d3SJohn Marino b is small, or the difference is small. Perform one subtraction
3086d7f5d3SJohn Marino followed by one division. If the gcd is found, stores it in gp and
3186d7f5d3SJohn Marino *gn, and returns zero. Otherwise, compute the reduced a and b, and
3286d7f5d3SJohn Marino return the new size. */
3386d7f5d3SJohn Marino
3486d7f5d3SJohn Marino /* FIXME: Check when the smaller number is a single limb, and invoke
3586d7f5d3SJohn Marino * mpn_gcd_1. */
3686d7f5d3SJohn Marino mp_size_t
mpn_gcd_subdiv_step(mp_ptr gp,mp_size_t * gn,mp_ptr ap,mp_ptr bp,mp_size_t n,mp_ptr tp)3786d7f5d3SJohn Marino mpn_gcd_subdiv_step (mp_ptr gp, mp_size_t *gn,
3886d7f5d3SJohn Marino mp_ptr ap, mp_ptr bp, mp_size_t n, mp_ptr tp)
3986d7f5d3SJohn Marino {
4086d7f5d3SJohn Marino mp_size_t an, bn;
4186d7f5d3SJohn Marino
4286d7f5d3SJohn Marino ASSERT (n > 0);
4386d7f5d3SJohn Marino ASSERT (ap[n-1] > 0 || bp[n-1] > 0);
4486d7f5d3SJohn Marino
4586d7f5d3SJohn Marino an = bn = n;
4686d7f5d3SJohn Marino MPN_NORMALIZE (ap, an);
4786d7f5d3SJohn Marino MPN_NORMALIZE (bp, bn);
4886d7f5d3SJohn Marino
4986d7f5d3SJohn Marino if (UNLIKELY (an == 0))
5086d7f5d3SJohn Marino {
5186d7f5d3SJohn Marino return_b:
5286d7f5d3SJohn Marino MPN_COPY (gp, bp, bn);
5386d7f5d3SJohn Marino *gn = bn;
5486d7f5d3SJohn Marino return 0;
5586d7f5d3SJohn Marino }
5686d7f5d3SJohn Marino else if (UNLIKELY (bn == 0))
5786d7f5d3SJohn Marino {
5886d7f5d3SJohn Marino return_a:
5986d7f5d3SJohn Marino MPN_COPY (gp, ap, an);
6086d7f5d3SJohn Marino *gn = an;
6186d7f5d3SJohn Marino return 0;
6286d7f5d3SJohn Marino }
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marino /* Arrange so that a > b, subtract an -= bn, and maintain
6586d7f5d3SJohn Marino normalization. */
6686d7f5d3SJohn Marino if (an < bn)
6786d7f5d3SJohn Marino MPN_PTR_SWAP (ap, an, bp, bn);
6886d7f5d3SJohn Marino else if (an == bn)
6986d7f5d3SJohn Marino {
7086d7f5d3SJohn Marino int c;
7186d7f5d3SJohn Marino MPN_CMP (c, ap, bp, an);
7286d7f5d3SJohn Marino if (UNLIKELY (c == 0))
7386d7f5d3SJohn Marino goto return_a;
7486d7f5d3SJohn Marino else if (c < 0)
7586d7f5d3SJohn Marino MP_PTR_SWAP (ap, bp);
7686d7f5d3SJohn Marino }
7786d7f5d3SJohn Marino
7886d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_sub (ap, ap, an, bp, bn));
7986d7f5d3SJohn Marino MPN_NORMALIZE (ap, an);
8086d7f5d3SJohn Marino ASSERT (an > 0);
8186d7f5d3SJohn Marino
8286d7f5d3SJohn Marino /* Arrange so that a > b, and divide a = q b + r */
8386d7f5d3SJohn Marino /* FIXME: an < bn happens when we have cancellation. If that is the
8486d7f5d3SJohn Marino common case, then we could reverse the roles of a and b to avoid
8586d7f5d3SJohn Marino the swap. */
8686d7f5d3SJohn Marino if (an < bn)
8786d7f5d3SJohn Marino MPN_PTR_SWAP (ap, an, bp, bn);
8886d7f5d3SJohn Marino else if (an == bn)
8986d7f5d3SJohn Marino {
9086d7f5d3SJohn Marino int c;
9186d7f5d3SJohn Marino MPN_CMP (c, ap, bp, an);
9286d7f5d3SJohn Marino if (UNLIKELY (c == 0))
9386d7f5d3SJohn Marino goto return_a;
9486d7f5d3SJohn Marino else if (c < 0)
9586d7f5d3SJohn Marino MP_PTR_SWAP (ap, bp);
9686d7f5d3SJohn Marino }
9786d7f5d3SJohn Marino
9886d7f5d3SJohn Marino mpn_tdiv_qr (tp, ap, 0, ap, an, bp, bn);
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marino if (mpn_zero_p (ap, bn))
10186d7f5d3SJohn Marino goto return_b;
10286d7f5d3SJohn Marino
10386d7f5d3SJohn Marino return bn;
10486d7f5d3SJohn Marino }
105