1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include "mpi-internal.h"
31#include "longlong.h"
32
33#ifndef UMUL_TIME
34#define UMUL_TIME 1
35#endif
36#ifndef UDIV_TIME
37#define UDIV_TIME UMUL_TIME
38#endif
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57mpi_limb_t
58mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
59 mpi_ptr_t np, mpi_size_t nsize, mpi_ptr_t dp, mpi_size_t dsize)
60{
61 mpi_limb_t most_significant_q_limb = 0;
62
63 switch (dsize) {
64 case 0:
65
66
67
68
69
70
71 return 1 / dsize;
72
73 case 1:
74 {
75 mpi_size_t i;
76 mpi_limb_t n1;
77 mpi_limb_t d;
78
79 d = dp[0];
80 n1 = np[nsize - 1];
81
82 if (n1 >= d) {
83 n1 -= d;
84 most_significant_q_limb = 1;
85 }
86
87 qp += qextra_limbs;
88 for (i = nsize - 2; i >= 0; i--)
89 udiv_qrnnd(qp[i], n1, n1, np[i], d);
90 qp -= qextra_limbs;
91
92 for (i = qextra_limbs - 1; i >= 0; i--)
93 udiv_qrnnd(qp[i], n1, n1, 0, d);
94
95 np[0] = n1;
96 }
97 break;
98
99 case 2:
100 {
101 mpi_size_t i;
102 mpi_limb_t n1, n0, n2;
103 mpi_limb_t d1, d0;
104
105 np += nsize - 2;
106 d1 = dp[1];
107 d0 = dp[0];
108 n1 = np[1];
109 n0 = np[0];
110
111 if (n1 >= d1 && (n1 > d1 || n0 >= d0)) {
112 sub_ddmmss(n1, n0, n1, n0, d1, d0);
113 most_significant_q_limb = 1;
114 }
115
116 for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--) {
117 mpi_limb_t q;
118 mpi_limb_t r;
119
120 if (i >= qextra_limbs)
121 np--;
122 else
123 np[0] = 0;
124
125 if (n1 == d1) {
126
127
128
129 q = ~(mpi_limb_t) 0;
130
131 r = n0 + d1;
132 if (r < d1) {
133 add_ssaaaa(n1, n0, r - d0,
134 np[0], 0, d0);
135 qp[i] = q;
136 continue;
137 }
138 n1 = d0 - (d0 != 0 ? 1 : 0);
139 n0 = -d0;
140 } else {
141 udiv_qrnnd(q, r, n1, n0, d1);
142 umul_ppmm(n1, n0, d0, q);
143 }
144
145 n2 = np[0];
146q_test:
147 if (n1 > r || (n1 == r && n0 > n2)) {
148
149 q--;
150 sub_ddmmss(n1, n0, n1, n0, 0, d0);
151 r += d1;
152 if (r >= d1)
153 goto q_test;
154 }
155
156 qp[i] = q;
157 sub_ddmmss(n1, n0, r, n2, n1, n0);
158 }
159 np[1] = n1;
160 np[0] = n0;
161 }
162 break;
163
164 default:
165 {
166 mpi_size_t i;
167 mpi_limb_t dX, d1, n0;
168
169 np += nsize - dsize;
170 dX = dp[dsize - 1];
171 d1 = dp[dsize - 2];
172 n0 = np[dsize - 1];
173
174 if (n0 >= dX) {
175 if (n0 > dX
176 || mpihelp_cmp(np, dp, dsize - 1) >= 0) {
177 mpihelp_sub_n(np, np, dp, dsize);
178 n0 = np[dsize - 1];
179 most_significant_q_limb = 1;
180 }
181 }
182
183 for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
184 mpi_limb_t q;
185 mpi_limb_t n1, n2;
186 mpi_limb_t cy_limb;
187
188 if (i >= qextra_limbs) {
189 np--;
190 n2 = np[dsize];
191 } else {
192 n2 = np[dsize - 1];
193 MPN_COPY_DECR(np + 1, np, dsize - 1);
194 np[0] = 0;
195 }
196
197 if (n0 == dX) {
198
199
200 q = ~(mpi_limb_t) 0;
201 } else {
202 mpi_limb_t r;
203
204 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
205 umul_ppmm(n1, n0, d1, q);
206
207 while (n1 > r
208 || (n1 == r
209 && n0 > np[dsize - 2])) {
210 q--;
211 r += dX;
212 if (r < dX)
213 break;
214 n1 -= n0 < d1;
215 n0 -= d1;
216 }
217 }
218
219
220
221
222 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
223
224 if (n2 != cy_limb) {
225 mpihelp_add_n(np, np, dp, dsize);
226 q--;
227 }
228
229 qp[i] = q;
230 n0 = np[dsize - 1];
231 }
232 }
233 }
234
235 return most_significant_q_limb;
236}
237