1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * %sccs.include.redist.c%
10 */
11
12 #if defined(LIBC_SCCS) && !defined(lint)
13 static char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 06/04/93";
14 #endif /* LIBC_SCCS and not lint */
15
16 /*
17 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
18 * section 4.3.1, pp. 257--259.
19 */
20
21 #include "quad.h"
22
23 #define B (1 << HALF_BITS) /* digit base */
24
25 /* Combine two `digits' to make a single two-digit number. */
26 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
27
28 /* select a type for digits in base B: use unsigned short if they fit */
29 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
30 typedef unsigned short digit;
31 #else
32 typedef u_long digit;
33 #endif
34
35 /*
36 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
37 * `fall out' the left (there never will be any such anyway).
38 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
39 */
40 static void
shl(register digit * p,register int len,register int sh)41 shl(register digit *p, register int len, register int sh)
42 {
43 register int i;
44
45 for (i = 0; i < len; i++)
46 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
47 p[i] = LHALF(p[i] << sh);
48 }
49
50 /*
51 * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
52 *
53 * We do this in base 2-sup-HALF_BITS, so that all intermediate products
54 * fit within u_long. As a consequence, the maximum length dividend and
55 * divisor are 4 `digits' in this base (they are shorter if they have
56 * leading zeros).
57 */
58 u_quad_t
__qdivrem(uq,vq,arq)59 __qdivrem(uq, vq, arq)
60 u_quad_t uq, vq, *arq;
61 {
62 union uu tmp;
63 digit *u, *v, *q;
64 register digit v1, v2;
65 u_long qhat, rhat, t;
66 int m, n, d, j, i;
67 digit uspace[5], vspace[5], qspace[5];
68
69 /*
70 * Take care of special cases: divide by zero, and u < v.
71 */
72 if (vq == 0) {
73 /* divide by zero. */
74 static volatile const unsigned int zero = 0;
75
76 tmp.ul[H] = tmp.ul[L] = 1 / zero;
77 if (arq)
78 *arq = uq;
79 return (tmp.q);
80 }
81 if (uq < vq) {
82 if (arq)
83 *arq = uq;
84 return (0);
85 }
86 u = &uspace[0];
87 v = &vspace[0];
88 q = &qspace[0];
89
90 /*
91 * Break dividend and divisor into digits in base B, then
92 * count leading zeros to determine m and n. When done, we
93 * will have:
94 * u = (u[1]u[2]...u[m+n]) sub B
95 * v = (v[1]v[2]...v[n]) sub B
96 * v[1] != 0
97 * 1 < n <= 4 (if n = 1, we use a different division algorithm)
98 * m >= 0 (otherwise u < v, which we already checked)
99 * m + n = 4
100 * and thus
101 * m = 4 - n <= 2
102 */
103 tmp.uq = uq;
104 u[0] = 0;
105 u[1] = HHALF(tmp.ul[H]);
106 u[2] = LHALF(tmp.ul[H]);
107 u[3] = HHALF(tmp.ul[L]);
108 u[4] = LHALF(tmp.ul[L]);
109 tmp.uq = vq;
110 v[1] = HHALF(tmp.ul[H]);
111 v[2] = LHALF(tmp.ul[H]);
112 v[3] = HHALF(tmp.ul[L]);
113 v[4] = LHALF(tmp.ul[L]);
114 for (n = 4; v[1] == 0; v++) {
115 if (--n == 1) {
116 u_long rbj; /* r*B+u[j] (not root boy jim) */
117 digit q1, q2, q3, q4;
118
119 /*
120 * Change of plan, per exercise 16.
121 * r = 0;
122 * for j = 1..4:
123 * q[j] = floor((r*B + u[j]) / v),
124 * r = (r*B + u[j]) % v;
125 * We unroll this completely here.
126 */
127 t = v[2]; /* nonzero, by definition */
128 q1 = u[1] / t;
129 rbj = COMBINE(u[1] % t, u[2]);
130 q2 = rbj / t;
131 rbj = COMBINE(rbj % t, u[3]);
132 q3 = rbj / t;
133 rbj = COMBINE(rbj % t, u[4]);
134 q4 = rbj / t;
135 if (arq)
136 *arq = rbj % t;
137 tmp.ul[H] = COMBINE(q1, q2);
138 tmp.ul[L] = COMBINE(q3, q4);
139 return (tmp.q);
140 }
141 }
142
143 /*
144 * By adjusting q once we determine m, we can guarantee that
145 * there is a complete four-digit quotient at &qspace[1] when
146 * we finally stop.
147 */
148 for (m = 4 - n; u[1] == 0; u++)
149 m--;
150 for (i = 4 - m; --i >= 0;)
151 q[i] = 0;
152 q += 4 - m;
153
154 /*
155 * Here we run Program D, translated from MIX to C and acquiring
156 * a few minor changes.
157 *
158 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
159 */
160 d = 0;
161 for (t = v[1]; t < B / 2; t <<= 1)
162 d++;
163 if (d > 0) {
164 shl(&u[0], m + n, d); /* u <<= d */
165 shl(&v[1], n - 1, d); /* v <<= d */
166 }
167 /*
168 * D2: j = 0.
169 */
170 j = 0;
171 v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
172 v2 = v[2]; /* for D3 */
173 do {
174 register digit uj0, uj1, uj2;
175
176 /*
177 * D3: Calculate qhat (\^q, in TeX notation).
178 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
179 * let rhat = (u[j]*B + u[j+1]) mod v[1].
180 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
181 * decrement qhat and increase rhat correspondingly.
182 * Note that if rhat >= B, v[2]*qhat < rhat*B.
183 */
184 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
185 uj1 = u[j + 1]; /* for D3 only */
186 uj2 = u[j + 2]; /* for D3 only */
187 if (uj0 == v1) {
188 qhat = B;
189 rhat = uj1;
190 goto qhat_too_big;
191 } else {
192 u_long n = COMBINE(uj0, uj1);
193 qhat = n / v1;
194 rhat = n % v1;
195 }
196 while (v2 * qhat > COMBINE(rhat, uj2)) {
197 qhat_too_big:
198 qhat--;
199 if ((rhat += v1) >= B)
200 break;
201 }
202 /*
203 * D4: Multiply and subtract.
204 * The variable `t' holds any borrows across the loop.
205 * We split this up so that we do not require v[0] = 0,
206 * and to eliminate a final special case.
207 */
208 for (t = 0, i = n; i > 0; i--) {
209 t = u[i + j] - v[i] * qhat - t;
210 u[i + j] = LHALF(t);
211 t = (B - HHALF(t)) & (B - 1);
212 }
213 t = u[j] - t;
214 u[j] = LHALF(t);
215 /*
216 * D5: test remainder.
217 * There is a borrow if and only if HHALF(t) is nonzero;
218 * in that (rare) case, qhat was too large (by exactly 1).
219 * Fix it by adding v[1..n] to u[j..j+n].
220 */
221 if (HHALF(t)) {
222 qhat--;
223 for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
224 t += u[i + j] + v[i];
225 u[i + j] = LHALF(t);
226 t = HHALF(t);
227 }
228 u[j] = LHALF(u[j] + t);
229 }
230 q[j] = qhat;
231 } while (++j <= m); /* D7: loop on j. */
232
233 /*
234 * If caller wants the remainder, we have to calculate it as
235 * u[m..m+n] >> d (this is at most n digits and thus fits in
236 * u[m+1..m+n], but we may need more source digits).
237 */
238 if (arq) {
239 if (d) {
240 for (i = m + n; i > m; --i)
241 u[i] = (u[i] >> d) |
242 LHALF(u[i - 1] << (HALF_BITS - d));
243 u[i] = 0;
244 }
245 tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
246 tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
247 *arq = tmp.q;
248 }
249
250 tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
251 tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
252 return (tmp.q);
253 }
254