1/* mpn_bdiv_q -- Hensel division with precomputed inverse, returning quotient.
2
3   Contributed to the GNU project by Torbjorn Granlund.
4
5   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
6   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
7   GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
8
9Copyright 2006, 2007, 2009 Free Software Foundation, Inc.
10
11This file is part of the GNU MP Library.
12
13The GNU MP Library is free software; you can redistribute it and/or modify
14it under the terms of the GNU Lesser General Public License as published by
15the Free Software Foundation; either version 3 of the License, or (at your
16option) any later version.
17
18The GNU MP Library is distributed in the hope that it will be useful, but
19WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21License for more details.
22
23You should have received a copy of the GNU Lesser General Public License
24along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
25
26#include "gmp.h"
27#include "gmp-impl.h"
28
29
30/* Computes Q = N / D mod B^n. */
31
32void
33mpn_bdiv_q (mp_ptr qp,
34	    mp_srcptr np, mp_size_t nn,
35	    mp_srcptr dp, mp_size_t dn,
36	    mp_ptr tp)
37{
38  mp_limb_t di;
39
40  if (BELOW_THRESHOLD (dn, DC_BDIV_Q_THRESHOLD))
41    {
42      MPN_COPY (tp, np, nn);
43      binvert_limb (di, dp[0]);  di = -di;
44      mpn_sbpi1_bdiv_q (qp, tp, nn, dp, dn, di);
45    }
46  else if (BELOW_THRESHOLD (dn, MU_BDIV_Q_THRESHOLD))
47    {
48      MPN_COPY (tp, np, nn);
49      binvert_limb (di, dp[0]);  di = -di;
50      mpn_dcpi1_bdiv_q (qp, tp, nn, dp, dn, di);
51    }
52  else
53    {
54      mpn_mu_bdiv_q (qp, np, nn, dp, dn, tp);
55    }
56  return;
57}
58
59mp_size_t
60mpn_bdiv_q_itch (mp_size_t nn, mp_size_t dn)
61{
62  if (BELOW_THRESHOLD (dn, MU_BDIV_Q_THRESHOLD))
63    return nn;
64  else
65    return mpn_mu_bdiv_q_itch (nn, dn);
66}
67