xref: /dflybsd-src/contrib/gmp/mpn/generic/dcpi1_div_q.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpn_dc_div_q -- divide-and-conquer division, returning exact quotient
286d7f5d3SJohn Marino    only.
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino    Contributed to the GNU project by Torbjorn Granlund and Marco Bodrato.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino    THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
786d7f5d3SJohn Marino    SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
886d7f5d3SJohn Marino    GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino Copyright 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino This file is part of the GNU MP Library.
1386d7f5d3SJohn Marino 
1486d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1586d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1686d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1786d7f5d3SJohn Marino option) any later version.
1886d7f5d3SJohn Marino 
1986d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
2086d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2186d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
2286d7f5d3SJohn Marino License for more details.
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2586d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2686d7f5d3SJohn Marino 
2786d7f5d3SJohn Marino #include "gmp.h"
2886d7f5d3SJohn Marino #include "gmp-impl.h"
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino mp_limb_t
mpn_dcpi1_div_q(mp_ptr qp,mp_ptr np,mp_size_t nn,mp_srcptr dp,mp_size_t dn,gmp_pi1_t * dinv)3286d7f5d3SJohn Marino mpn_dcpi1_div_q (mp_ptr qp, mp_ptr np, mp_size_t nn,
3386d7f5d3SJohn Marino 		 mp_srcptr dp, mp_size_t dn, gmp_pi1_t *dinv)
3486d7f5d3SJohn Marino {
3586d7f5d3SJohn Marino   mp_ptr tp, wp;
3686d7f5d3SJohn Marino   mp_limb_t qh;
3786d7f5d3SJohn Marino   mp_size_t qn;
3886d7f5d3SJohn Marino   TMP_DECL;
3986d7f5d3SJohn Marino 
4086d7f5d3SJohn Marino   TMP_MARK;
4186d7f5d3SJohn Marino 
4286d7f5d3SJohn Marino   ASSERT (dn >= 6);
4386d7f5d3SJohn Marino   ASSERT (nn - dn >= 3);
4486d7f5d3SJohn Marino   ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);
4586d7f5d3SJohn Marino 
4686d7f5d3SJohn Marino   tp = TMP_SALLOC_LIMBS (nn + 1);
4786d7f5d3SJohn Marino   MPN_COPY (tp + 1, np, nn);
4886d7f5d3SJohn Marino   tp[0] = 0;
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino   qn = nn - dn;
5186d7f5d3SJohn Marino   wp = TMP_SALLOC_LIMBS (qn + 1);
5286d7f5d3SJohn Marino 
5386d7f5d3SJohn Marino   qh = mpn_dcpi1_divappr_q (wp, tp, nn + 1, dp, dn, dinv);
5486d7f5d3SJohn Marino 
5586d7f5d3SJohn Marino   if (wp[0] == 0)
5686d7f5d3SJohn Marino     {
5786d7f5d3SJohn Marino       mp_limb_t cy;
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino       if (qn > dn)
6086d7f5d3SJohn Marino 	mpn_mul (tp, wp + 1, qn, dp, dn);
6186d7f5d3SJohn Marino       else
6286d7f5d3SJohn Marino 	mpn_mul (tp, dp, dn, wp + 1, qn);
6386d7f5d3SJohn Marino 
6486d7f5d3SJohn Marino       cy = (qh != 0) ? mpn_add_n (tp + qn, tp + qn, dp, dn) : 0;
6586d7f5d3SJohn Marino 
6686d7f5d3SJohn Marino       if (cy || mpn_cmp (tp, np, nn) > 0) /* At most is wrong by one, no cycle. */
6786d7f5d3SJohn Marino 	qh -= mpn_sub_1 (qp, wp + 1, qn, 1);
6886d7f5d3SJohn Marino       else /* Same as below */
6986d7f5d3SJohn Marino 	MPN_COPY (qp, wp + 1, qn);
7086d7f5d3SJohn Marino     }
7186d7f5d3SJohn Marino   else
7286d7f5d3SJohn Marino     MPN_COPY (qp, wp + 1, qn);
7386d7f5d3SJohn Marino 
7486d7f5d3SJohn Marino   TMP_FREE;
7586d7f5d3SJohn Marino   return qh;
7686d7f5d3SJohn Marino }
77