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-2011 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 either:
17 * the GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
23 * the GNU General Public License as published by the Free Software
24 Foundation; either version 2 of the License, or (at your option) any
27 or both in parallel, as here.
29 The GNU MP Library is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
34 You should have received copies of the GNU General Public License and the
35 GNU Lesser General Public License along with the GNU MP Library. If not,
36 see https://www.gnu.org/licenses/. */
43 mpn_dcpi1_bdiv_q_n_itch (mp_size_t n)
45 /* NOTE: Depends on mullo_n interface */
49 /* Computes Q = N / D mod B^n, destroys N.
56 mpn_dcpi1_bdiv_q_n (mp_ptr qp,
57 mp_ptr np, mp_srcptr dp, mp_size_t n,
58 mp_limb_t dinv, mp_ptr tp)
60 while (ABOVE_THRESHOLD (n, DC_BDIV_Q_THRESHOLD))
65 lo = n >> 1; /* floor(n/2) */
66 hi = n - lo; /* ceil(n/2) */
68 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, lo, dinv, tp);
70 mpn_mullo_n (tp, qp, dp + hi, lo);
71 mpn_sub_n (np + hi, np + hi, tp, lo);
75 cy += mpn_submul_1 (np + lo, qp, lo, dp[lo]);
82 mpn_sbpi1_bdiv_q (qp, np, n, dp, n, dinv);
85 /* Computes Q = N / D mod B^nn, destroys N.
92 mpn_dcpi1_bdiv_q (mp_ptr qp,
93 mp_ptr np, mp_size_t nn,
94 mp_srcptr dp, mp_size_t dn,
105 ASSERT (nn - dn >= 0);
108 tp = TMP_SALLOC_LIMBS (dn);
114 /* Reduce qn mod dn in a super-efficient manner. */
119 /* Perform the typically smaller block first. */
120 if (BELOW_THRESHOLD (qn, DC_BDIV_QR_THRESHOLD))
121 cy = mpn_sbpi1_bdiv_qr (qp, np, 2 * qn, dp, qn, dinv);
123 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, qn, dinv, tp);
128 mpn_mul (tp, qp, qn, dp + qn, dn - qn);
130 mpn_mul (tp, dp + qn, dn - qn, qp, qn);
131 mpn_incr_u (tp + qn, cy);
133 mpn_sub (np + qn, np + qn, nn - qn, tp, dn);
143 mpn_sub_1 (np + dn, np + dn, qn - dn, cy);
144 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, dn, dinv, tp);
149 mpn_dcpi1_bdiv_q_n (qp, np, dp, dn, dinv, tp);
153 if (BELOW_THRESHOLD (qn, DC_BDIV_Q_THRESHOLD))
154 mpn_sbpi1_bdiv_q (qp, np, qn, dp, qn, dinv);
156 mpn_dcpi1_bdiv_q_n (qp, np, dp, qn, dinv, tp);