1 /* mpn_dcpi1_bdiv_q -- divide-and-conquer Hensel division with precomputed
2 inverse, returning quotient.
4 Contributed to the GNU project by Niels Möller and Torbjorn Granlund.
6 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
7 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
8 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
12 This file is part of the GNU MP Library.
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
22 License for more details.
24 You should have received a copy of the GNU Lesser General Public License
25 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
33 mpn_dcpi1_bdiv_q_n_itch (mp_size_t n)
35 /* NOTE: Depends on mullo_n interface */
39 /* Computes Q = N / D mod B^n, destroys N.
46 mpn_dcpi1_bdiv_q_n (mp_ptr qp,
47 mp_ptr np, mp_srcptr dp, mp_size_t n,
48 mp_limb_t dinv, mp_ptr tp)
50 while (ABOVE_THRESHOLD (n, DC_BDIV_Q_THRESHOLD))
55 lo = n >> 1; /* floor(n/2) */
56 hi = n - lo; /* ceil(n/2) */
58 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, lo, dinv, tp);
60 mpn_mullo_n (tp, qp, dp + hi, lo);
61 mpn_sub_n (np + hi, np + hi, tp, lo);
65 cy += mpn_submul_1 (np + lo, qp, lo, dp[lo]);
72 mpn_sbpi1_bdiv_q (qp, np, n, dp, n, dinv);
75 /* Computes Q = N / D mod B^nn, destroys N.
82 mpn_dcpi1_bdiv_q (mp_ptr qp,
83 mp_ptr np, mp_size_t nn,
84 mp_srcptr dp, mp_size_t dn,
95 ASSERT (nn - dn >= 0);
98 tp = TMP_SALLOC_LIMBS (dn);
104 /* Reduce qn mod dn in a super-efficient manner. */
109 /* Perform the typically smaller block first. */
110 if (BELOW_THRESHOLD (qn, DC_BDIV_QR_THRESHOLD))
111 cy = mpn_sbpi1_bdiv_qr (qp, np, 2 * qn, dp, qn, dinv);
113 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, qn, dinv, tp);
118 mpn_mul (tp, qp, qn, dp + qn, dn - qn);
120 mpn_mul (tp, dp + qn, dn - qn, qp, qn);
121 mpn_incr_u (tp + qn, cy);
123 mpn_sub (np + qn, np + qn, nn - qn, tp, dn);
133 mpn_sub_1 (np + dn, np + dn, qn, cy);
134 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, dn, dinv, tp);
139 mpn_dcpi1_bdiv_q_n (qp, np, dp, dn, dinv, tp);
143 if (BELOW_THRESHOLD (qn, DC_BDIV_Q_THRESHOLD))
144 mpn_sbpi1_bdiv_q (qp, np, qn, dp, qn, dinv);
146 mpn_dcpi1_bdiv_q_n (qp, np, dp, qn, dinv, tp);