1137015Sdes/* mpn_bdiv_q -- Hensel division with precomputed inverse, returning quotient.
2124208Sdes
3106121Sdes   Contributed to the GNU project by Torbjorn Granlund.
4106121Sdes
5106121Sdes   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
6106121Sdes   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
7106121Sdes   GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
8106121Sdes
9106121SdesCopyright 2006, 2007, 2009 Free Software Foundation, Inc.
10106121Sdes
11106121SdesThis file is part of the GNU MP Library.
12106121Sdes
13106121SdesThe GNU MP Library is free software; you can redistribute it and/or modify
14106121Sdesit under the terms of the GNU Lesser General Public License as published by
15106121Sdesthe Free Software Foundation; either version 3 of the License, or (at your
16106121Sdesoption) any later version.
17106121Sdes
18106121SdesThe GNU MP Library is distributed in the hope that it will be useful, but
19106121SdesWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20106121Sdesor FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21106121SdesLicense for more details.
22106121Sdes
23106121SdesYou should have received a copy of the GNU Lesser General Public License
24106121Sdesalong with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
25106121Sdes
26106121Sdes#include "gmp.h"
27106121Sdes#include "gmp-impl.h"
2898937Sdes
29113908Sdes
30124208Sdes/* Computes Q = N / D mod B^n. */
31124208Sdes
32124208Sdesvoid
33124208Sdesmpn_bdiv_q (mp_ptr qp,
34124208Sdes	    mp_srcptr np, mp_size_t nn,
35124208Sdes	    mp_srcptr dp, mp_size_t dn,
36124208Sdes	    mp_ptr tp)
37124208Sdes{
38124208Sdes  mp_limb_t di;
39124208Sdes
40124208Sdes  if (BELOW_THRESHOLD (dn, DC_BDIV_Q_THRESHOLD))
41124208Sdes    {
42124208Sdes      MPN_COPY (tp, np, nn);
43124208Sdes      binvert_limb (di, dp[0]);  di = -di;
44113908Sdes      mpn_sbpi1_bdiv_q (qp, tp, nn, dp, dn, di);
45113908Sdes    }
46113908Sdes  else if (BELOW_THRESHOLD (dn, MU_BDIV_Q_THRESHOLD))
47113908Sdes    {
48113908Sdes      MPN_COPY (tp, np, nn);
49113908Sdes      binvert_limb (di, dp[0]);  di = -di;
50113908Sdes      mpn_dcpi1_bdiv_q (qp, tp, nn, dp, dn, di);
51113908Sdes    }
52113908Sdes  else
53113908Sdes    {
54126274Sdes      mpn_mu_bdiv_q (qp, np, nn, dp, dn, tp);
55126274Sdes    }
56126274Sdes  return;
57126274Sdes}
58126274Sdes
59126274Sdesmp_size_t
60126274Sdesmpn_bdiv_q_itch (mp_size_t nn, mp_size_t dn)
61126274Sdes{
62126274Sdes  if (BELOW_THRESHOLD (dn, MU_BDIV_Q_THRESHOLD))
63126274Sdes    return nn;
64124208Sdes  else
65126274Sdes    return mpn_mu_bdiv_q_itch (nn, dn);
66137015Sdes}
67137015Sdes