1 /* mpn_mu_bdiv_q(qp,np,nn,dp,dn,tp) -- Compute {np,nn} / {dp,dn} mod B^nn.
2 storing the result in {qp,nn}. Overlap allowed between Q and N; all other
5 Contributed to the GNU project by Torbjorn Granlund.
7 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
8 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
11 Copyright 2005-2007, 2009, 2010 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of either:
18 * the GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 3 of the License, or (at your
20 option) any later version.
24 * the GNU General Public License as published by the Free Software
25 Foundation; either version 2 of the License, or (at your option) any
28 or both in parallel, as here.
30 The GNU MP Library is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35 You should have received copies of the GNU General Public License and the
36 GNU Lesser General Public License along with the GNU MP Library. If not,
37 see https://www.gnu.org/licenses/. */
41 The idea of the algorithm used herein is to compute a smaller inverted value
42 than used in the standard Barrett algorithm, and thus save time in the
43 Newton iterations, and pay just a small price when using the inverted value
44 for developing quotient bits. This algorithm was presented at ICMS 2006.
59 scratch space as determined by mpn_mu_bdiv_q_itch(nn,dn).
61 Write quotient to Q = {qp,nn}.
63 FIXME: When iterating, perhaps do the small step before loop, not after.
64 FIXME: Try to avoid the scalar divisions when computing inverse size.
65 FIXME: Trim allocation for (qn > dn) case, 3*dn might be possible. In
66 particular, when dn==in, tp and rp could use the same space.
67 FIXME: Trim final quotient calculation to qn limbs of precision.
70 mpn_mu_bdiv_q (mp_ptr qp
,
71 mp_srcptr np
, mp_size_t nn
,
72 mp_srcptr dp
, mp_size_t dn
,
89 /* |_______________________| dividend
92 #define ip scratch /* in */
93 #define rp (scratch + in) /* dn or rest >= binvert_itch(in) */
94 #define tp (scratch + in + dn) /* dn+in or next_size(dn) */
95 #define scratch_out (scratch + in + dn + tn) /* mulmod_bnm1_itch(next_size(dn)) */
97 /* Compute an inverse size that is a nice partition of the quotient. */
98 b
= (qn
- 1) / dn
+ 1; /* ceil(qn/dn), number of blocks */
99 in
= (qn
- 1) / b
+ 1; /* ceil(qn/b) = ceil(qn / ceil(qn/dn)) */
101 /* Some notes on allocation:
103 When in = dn, R dies when mpn_mullo returns, if in < dn the low in
104 limbs of R dies at that point. We could save memory by letting T live
105 just under R, and let the upper part of T expand into R. These changes
106 should reduce itch to perhaps 3dn.
109 mpn_binvert (ip
, dp
, in
, rp
);
113 MPN_COPY (rp
, np
, dn
);
115 mpn_mullo_n (qp
, rp
, ip
, in
);
120 if (BELOW_THRESHOLD (in
, MUL_TO_MULMOD_BNM1_FOR_2NXN_THRESHOLD
))
121 mpn_mul (tp
, dp
, dn
, qp
, in
); /* mulhi, need tp[dn+in-1...in] */
124 tn
= mpn_mulmod_bnm1_next_size (dn
);
125 mpn_mulmod_bnm1 (tp
, tn
, dp
, dn
, qp
, in
, scratch_out
);
126 wn
= dn
+ in
- tn
; /* number of wrapped limbs */
129 c0
= mpn_sub_n (tp
+ tn
, tp
, rp
, wn
);
130 mpn_decr_u (tp
+ wn
, c0
);
137 /* Subtract tp[dn-1...in] from partial remainder. */
138 cy
+= mpn_sub_n (rp
, rp
+ in
, tp
+ in
, dn
- in
);
141 mpn_incr_u (tp
+ dn
, 1);
145 /* Subtract tp[dn+in-1...dn] from dividend. */
146 cy
= mpn_sub_nc (rp
+ dn
- in
, np
, tp
+ dn
, in
, cy
);
148 mpn_mullo_n (qp
, rp
, ip
, in
);
152 /* Generate last qn limbs.
153 FIXME: It should be possible to limit precision here, since qn is
154 typically somewhat smaller than dn. No big gains expected. */
156 if (BELOW_THRESHOLD (in
, MUL_TO_MULMOD_BNM1_FOR_2NXN_THRESHOLD
))
157 mpn_mul (tp
, dp
, dn
, qp
, in
); /* mulhi, need tp[qn+in-1...in] */
160 tn
= mpn_mulmod_bnm1_next_size (dn
);
161 mpn_mulmod_bnm1 (tp
, tn
, dp
, dn
, qp
, in
, scratch_out
);
162 wn
= dn
+ in
- tn
; /* number of wrapped limbs */
165 c0
= mpn_sub_n (tp
+ tn
, tp
, rp
, wn
);
166 mpn_decr_u (tp
+ wn
, c0
);
173 cy
+= mpn_sub_n (rp
, rp
+ in
, tp
+ in
, dn
- in
);
176 mpn_incr_u (tp
+ dn
, 1);
181 mpn_sub_nc (rp
+ dn
- in
, np
, tp
+ dn
, qn
- (dn
- in
), cy
);
182 mpn_mullo_n (qp
, rp
, ip
, qn
);
191 /* |_______________________| dividend
192 |________________| divisor */
194 #define ip scratch /* in */
195 #define tp (scratch + in) /* qn+in or next_size(qn) or rest >= binvert_itch(in) */
196 #define scratch_out (scratch + in + tn)/* mulmod_bnm1_itch(next_size(qn)) */
198 /* Compute half-sized inverse. */
201 mpn_binvert (ip
, dp
, in
, tp
);
203 mpn_mullo_n (qp
, np
, ip
, in
); /* low `in' quotient limbs */
205 if (BELOW_THRESHOLD (in
, MUL_TO_MULMOD_BNM1_FOR_2NXN_THRESHOLD
))
206 mpn_mul (tp
, dp
, qn
, qp
, in
); /* mulhigh */
209 tn
= mpn_mulmod_bnm1_next_size (qn
);
210 mpn_mulmod_bnm1 (tp
, tn
, dp
, qn
, qp
, in
, scratch_out
);
211 wn
= qn
+ in
- tn
; /* number of wrapped limbs */
214 c0
= mpn_cmp (tp
, np
, wn
) < 0;
215 mpn_decr_u (tp
+ wn
, c0
);
219 mpn_sub_n (tp
, np
+ in
, tp
+ in
, qn
- in
);
220 mpn_mullo_n (qp
+ in
, tp
, ip
, qn
- in
); /* high qn-in quotient limbs */
229 mpn_mu_bdiv_q_itch (mp_size_t nn
, mp_size_t dn
)
231 mp_size_t qn
, in
, tn
, itch_binvert
, itch_out
, itches
;
234 ASSERT_ALWAYS (DC_BDIV_Q_THRESHOLD
< MU_BDIV_Q_THRESHOLD
);
240 b
= (qn
- 1) / dn
+ 1; /* ceil(qn/dn), number of blocks */
241 in
= (qn
- 1) / b
+ 1; /* ceil(qn/b) = ceil(qn / ceil(qn/dn)) */
242 if (BELOW_THRESHOLD (in
, MUL_TO_MULMOD_BNM1_FOR_2NXN_THRESHOLD
))
249 tn
= mpn_mulmod_bnm1_next_size (dn
);
250 itch_out
= mpn_mulmod_bnm1_itch (tn
, dn
, in
);
252 itches
= dn
+ tn
+ itch_out
;
257 if (BELOW_THRESHOLD (in
, MUL_TO_MULMOD_BNM1_FOR_2NXN_THRESHOLD
))
264 tn
= mpn_mulmod_bnm1_next_size (qn
);
265 itch_out
= mpn_mulmod_bnm1_itch (tn
, qn
, in
);
267 itches
= tn
+ itch_out
;
270 itch_binvert
= mpn_binvert_itch (in
);
271 return in
+ MAX (itches
, itch_binvert
);