1 /* mpn_dcpi1_bdiv_q -- divide-and-conquer Hensel division with precomputed
2 inverse, returning quotient.
3
4 Contributed to the GNU project by Niels Möller and Torbjorn Granlund.
5
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.
9
10 Copyright 2006, 2007, 2009-2011, 2017 Free Software Foundation, Inc.
11
12 This file is part of the GNU MP Library.
13
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of either:
16
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.
20
21 or
22
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.
26
27 or both in parallel, as here.
28
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.
33
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/. */
37
38 #include "gmp-impl.h"
39
40
41 #if 0 /* unused, so leave out for now */
42 static mp_size_t
43 mpn_dcpi1_bdiv_q_n_itch (mp_size_t n)
44 {
45 /* NOTE: Depends on mullo_n and mpn_dcpi1_bdiv_qr_n interface */
46 return n;
47 }
48 #endif
49
50 /* Computes Q = - N / D mod B^n, destroys N.
51
52 N = {np,n}
53 D = {dp,n}
54 */
55
56 static void
57 mpn_dcpi1_bdiv_q_n (mp_ptr qp,
58 mp_ptr np, mp_srcptr dp, mp_size_t n,
59 mp_limb_t dinv, mp_ptr tp)
60 {
61 while (ABOVE_THRESHOLD (n, DC_BDIV_Q_THRESHOLD))
62 {
63 mp_size_t lo, hi;
64 mp_limb_t cy;
65
66 lo = n >> 1; /* floor(n/2) */
67 hi = n - lo; /* ceil(n/2) */
68
69 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, lo, dinv, tp);
70
71 mpn_mullo_n (tp, qp, dp + hi, lo);
72 mpn_add_n (np + hi, np + hi, tp, lo);
73
74 if (lo < hi)
75 {
76 cy += mpn_addmul_1 (np + lo, qp, lo, dp[lo]);
77 np[n - 1] += cy;
78 }
79 qp += lo;
80 np += lo;
81 n -= lo;
82 }
83 mpn_sbpi1_bdiv_q (qp, np, n, dp, n, dinv);
84 }
85
86 /* Computes Q = - N / D mod B^nn, destroys N.
87
88 N = {np,nn}
89 D = {dp,dn}
90 */
91
92 void
93 mpn_dcpi1_bdiv_q (mp_ptr qp,
94 mp_ptr np, mp_size_t nn,
95 mp_srcptr dp, mp_size_t dn,
96 mp_limb_t dinv)
97 {
98 mp_size_t qn;
99 mp_limb_t cy;
100 mp_ptr tp;
101 TMP_DECL;
102
103 TMP_MARK;
104
105 ASSERT (dn >= 2);
106 ASSERT (nn - dn >= 0);
107 ASSERT (dp[0] & 1);
108
109 tp = TMP_SALLOC_LIMBS (dn);
110
111 qn = nn;
112
113 if (qn > dn)
114 {
115 /* Reduce qn mod dn in a super-efficient manner. */
116 do
117 qn -= dn;
118 while (qn > dn);
119
120 /* Perform the typically smaller block first. */
121 if (BELOW_THRESHOLD (qn, DC_BDIV_QR_THRESHOLD))
122 cy = mpn_sbpi1_bdiv_qr (qp, np, 2 * qn, dp, qn, dinv);
123 else
124 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, qn, dinv, tp);
125
126 if (qn != dn)
127 {
128 if (qn > dn - qn)
129 mpn_mul (tp, qp, qn, dp + qn, dn - qn);
130 else
131 mpn_mul (tp, dp + qn, dn - qn, qp, qn);
132 mpn_incr_u (tp + qn, cy);
133
134 mpn_add (np + qn, np + qn, nn - qn, tp, dn);
135 cy = 0;
136 }
137
138 np += qn;
139 qp += qn;
140
141 qn = nn - qn;
142 while (qn > dn)
143 {
144 mpn_add_1 (np + dn, np + dn, qn - dn, cy);
145 cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, dn, dinv, tp);
146 qp += dn;
147 np += dn;
148 qn -= dn;
149 }
150 mpn_dcpi1_bdiv_q_n (qp, np, dp, dn, dinv, tp);
151 }
152 else
153 {
154 if (BELOW_THRESHOLD (qn, DC_BDIV_Q_THRESHOLD))
155 mpn_sbpi1_bdiv_q (qp, np, qn, dp, qn, dinv);
156 else
157 mpn_dcpi1_bdiv_q_n (qp, np, dp, qn, dinv, tp);
158 }
159
160 TMP_FREE;
161 }