beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / dcpi1_bdiv_q.c
bloba7b86c96d44b48b844120c983ee809defcd6482d
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
25 later version.
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
32 for more details.
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/. */
38 #include "gmp.h"
39 #include "gmp-impl.h"
42 mp_size_t
43 mpn_dcpi1_bdiv_q_n_itch (mp_size_t n)
45 /* NOTE: Depends on mullo_n interface */
46 return n;
49 /* Computes Q = N / D mod B^n, destroys N.
51 N = {np,n}
52 D = {dp,n}
55 void
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))
62 mp_size_t lo, hi;
63 mp_limb_t cy;
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);
73 if (lo < hi)
75 cy += mpn_submul_1 (np + lo, qp, lo, dp[lo]);
76 np[n - 1] -= cy;
78 qp += lo;
79 np += lo;
80 n -= lo;
82 mpn_sbpi1_bdiv_q (qp, np, n, dp, n, dinv);
85 /* Computes Q = N / D mod B^nn, destroys N.
87 N = {np,nn}
88 D = {dp,dn}
91 void
92 mpn_dcpi1_bdiv_q (mp_ptr qp,
93 mp_ptr np, mp_size_t nn,
94 mp_srcptr dp, mp_size_t dn,
95 mp_limb_t dinv)
97 mp_size_t qn;
98 mp_limb_t cy;
99 mp_ptr tp;
100 TMP_DECL;
102 TMP_MARK;
104 ASSERT (dn >= 2);
105 ASSERT (nn - dn >= 0);
106 ASSERT (dp[0] & 1);
108 tp = TMP_SALLOC_LIMBS (dn);
110 qn = nn;
112 if (qn > dn)
114 /* Reduce qn mod dn in a super-efficient manner. */
116 qn -= dn;
117 while (qn > dn);
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);
122 else
123 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, qn, dinv, tp);
125 if (qn != dn)
127 if (qn > dn - qn)
128 mpn_mul (tp, qp, qn, dp + qn, dn - qn);
129 else
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);
134 cy = 0;
137 np += qn;
138 qp += qn;
140 qn = nn - qn;
141 while (qn > 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);
145 qp += dn;
146 np += dn;
147 qn -= dn;
149 mpn_dcpi1_bdiv_q_n (qp, np, dp, dn, dinv, tp);
151 else
153 if (BELOW_THRESHOLD (qn, DC_BDIV_Q_THRESHOLD))
154 mpn_sbpi1_bdiv_q (qp, np, qn, dp, qn, dinv);
155 else
156 mpn_dcpi1_bdiv_q_n (qp, np, dp, qn, dinv, tp);
159 TMP_FREE;