qdivrem.c revision 1.1 1 1.1 christos /* $NetBSD: qdivrem.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 1992, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * This software was developed by the Computer Systems Engineering group
8 1.1 christos * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 christos * contributed to Berkeley.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer.
16 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer in the
18 1.1 christos * documentation and/or other materials provided with the distribution.
19 1.1 christos * 3. Neither the name of the University nor the names of its contributors
20 1.1 christos * may be used to endorse or promote products derived from this software
21 1.1 christos * without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 christos * SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 christos #if 0
39 1.1 christos static char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 6/4/93";
40 1.1 christos #else
41 1.1 christos __RCSID("$NetBSD: qdivrem.c,v 1.1 2005/12/20 19:28:51 christos Exp $");
42 1.1 christos #endif
43 1.1 christos #endif /* LIBC_SCCS and not lint */
44 1.1 christos
45 1.1 christos /*
46 1.1 christos * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
47 1.1 christos * section 4.3.1, pp. 257--259.
48 1.1 christos */
49 1.1 christos
50 1.1 christos #include "quad.h"
51 1.1 christos
52 1.1 christos #define B ((int)1 << HALF_BITS) /* digit base */
53 1.1 christos
54 1.1 christos /* Combine two `digits' to make a single two-digit number. */
55 1.1 christos #define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b))
56 1.1 christos
57 1.1 christos /* select a type for digits in base B: use unsigned short if they fit */
58 1.1 christos #if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff
59 1.1 christos typedef unsigned short digit;
60 1.1 christos #else
61 1.1 christos typedef u_int digit;
62 1.1 christos #endif
63 1.1 christos
64 1.1 christos static void shl __P((digit *p, int len, int sh));
65 1.1 christos
66 1.1 christos /*
67 1.1 christos * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
68 1.1 christos *
69 1.1 christos * We do this in base 2-sup-HALF_BITS, so that all intermediate products
70 1.1 christos * fit within u_int. As a consequence, the maximum length dividend and
71 1.1 christos * divisor are 4 `digits' in this base (they are shorter if they have
72 1.1 christos * leading zeros).
73 1.1 christos */
74 1.1 christos u_quad_t
75 1.1 christos __qdivrem(uq, vq, arq)
76 1.1 christos u_quad_t uq, vq, *arq;
77 1.1 christos {
78 1.1 christos union uu tmp;
79 1.1 christos digit *u, *v, *q;
80 1.1 christos digit v1, v2;
81 1.1 christos u_int qhat, rhat, t;
82 1.1 christos int m, n, d, j, i;
83 1.1 christos digit uspace[5], vspace[5], qspace[5];
84 1.1 christos
85 1.1 christos /*
86 1.1 christos * Take care of special cases: divide by zero, and u < v.
87 1.1 christos */
88 1.1 christos if (vq == 0) {
89 1.1 christos /* divide by zero. */
90 1.1 christos static volatile const unsigned int zero = 0;
91 1.1 christos
92 1.1 christos tmp.ul[H] = tmp.ul[L] = 1 / zero;
93 1.1 christos if (arq)
94 1.1 christos *arq = uq;
95 1.1 christos return (tmp.q);
96 1.1 christos }
97 1.1 christos if (uq < vq) {
98 1.1 christos if (arq)
99 1.1 christos *arq = uq;
100 1.1 christos return (0);
101 1.1 christos }
102 1.1 christos u = &uspace[0];
103 1.1 christos v = &vspace[0];
104 1.1 christos q = &qspace[0];
105 1.1 christos
106 1.1 christos /*
107 1.1 christos * Break dividend and divisor into digits in base B, then
108 1.1 christos * count leading zeros to determine m and n. When done, we
109 1.1 christos * will have:
110 1.1 christos * u = (u[1]u[2]...u[m+n]) sub B
111 1.1 christos * v = (v[1]v[2]...v[n]) sub B
112 1.1 christos * v[1] != 0
113 1.1 christos * 1 < n <= 4 (if n = 1, we use a different division algorithm)
114 1.1 christos * m >= 0 (otherwise u < v, which we already checked)
115 1.1 christos * m + n = 4
116 1.1 christos * and thus
117 1.1 christos * m = 4 - n <= 2
118 1.1 christos */
119 1.1 christos tmp.uq = uq;
120 1.1 christos u[0] = 0;
121 1.1 christos u[1] = (digit)HHALF(tmp.ul[H]);
122 1.1 christos u[2] = (digit)LHALF(tmp.ul[H]);
123 1.1 christos u[3] = (digit)HHALF(tmp.ul[L]);
124 1.1 christos u[4] = (digit)LHALF(tmp.ul[L]);
125 1.1 christos tmp.uq = vq;
126 1.1 christos v[1] = (digit)HHALF(tmp.ul[H]);
127 1.1 christos v[2] = (digit)LHALF(tmp.ul[H]);
128 1.1 christos v[3] = (digit)HHALF(tmp.ul[L]);
129 1.1 christos v[4] = (digit)LHALF(tmp.ul[L]);
130 1.1 christos for (n = 4; v[1] == 0; v++) {
131 1.1 christos if (--n == 1) {
132 1.1 christos u_int rbj; /* r*B+u[j] (not root boy jim) */
133 1.1 christos digit q1, q2, q3, q4;
134 1.1 christos
135 1.1 christos /*
136 1.1 christos * Change of plan, per exercise 16.
137 1.1 christos * r = 0;
138 1.1 christos * for j = 1..4:
139 1.1 christos * q[j] = floor((r*B + u[j]) / v),
140 1.1 christos * r = (r*B + u[j]) % v;
141 1.1 christos * We unroll this completely here.
142 1.1 christos */
143 1.1 christos t = v[2]; /* nonzero, by definition */
144 1.1 christos q1 = (digit)(u[1] / t);
145 1.1 christos rbj = COMBINE(u[1] % t, u[2]);
146 1.1 christos q2 = (digit)(rbj / t);
147 1.1 christos rbj = COMBINE(rbj % t, u[3]);
148 1.1 christos q3 = (digit)(rbj / t);
149 1.1 christos rbj = COMBINE(rbj % t, u[4]);
150 1.1 christos q4 = (digit)(rbj / t);
151 1.1 christos if (arq)
152 1.1 christos *arq = rbj % t;
153 1.1 christos tmp.ul[H] = COMBINE(q1, q2);
154 1.1 christos tmp.ul[L] = COMBINE(q3, q4);
155 1.1 christos return (tmp.q);
156 1.1 christos }
157 1.1 christos }
158 1.1 christos
159 1.1 christos /*
160 1.1 christos * By adjusting q once we determine m, we can guarantee that
161 1.1 christos * there is a complete four-digit quotient at &qspace[1] when
162 1.1 christos * we finally stop.
163 1.1 christos */
164 1.1 christos for (m = 4 - n; u[1] == 0; u++)
165 1.1 christos m--;
166 1.1 christos for (i = 4 - m; --i >= 0;)
167 1.1 christos q[i] = 0;
168 1.1 christos q += 4 - m;
169 1.1 christos
170 1.1 christos /*
171 1.1 christos * Here we run Program D, translated from MIX to C and acquiring
172 1.1 christos * a few minor changes.
173 1.1 christos *
174 1.1 christos * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
175 1.1 christos */
176 1.1 christos d = 0;
177 1.1 christos for (t = v[1]; t < B / 2; t <<= 1)
178 1.1 christos d++;
179 1.1 christos if (d > 0) {
180 1.1 christos shl(&u[0], m + n, d); /* u <<= d */
181 1.1 christos shl(&v[1], n - 1, d); /* v <<= d */
182 1.1 christos }
183 1.1 christos /*
184 1.1 christos * D2: j = 0.
185 1.1 christos */
186 1.1 christos j = 0;
187 1.1 christos v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
188 1.1 christos v2 = v[2]; /* for D3 */
189 1.1 christos do {
190 1.1 christos digit uj0, uj1, uj2;
191 1.1 christos
192 1.1 christos /*
193 1.1 christos * D3: Calculate qhat (\^q, in TeX notation).
194 1.1 christos * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
195 1.1 christos * let rhat = (u[j]*B + u[j+1]) mod v[1].
196 1.1 christos * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
197 1.1 christos * decrement qhat and increase rhat correspondingly.
198 1.1 christos * Note that if rhat >= B, v[2]*qhat < rhat*B.
199 1.1 christos */
200 1.1 christos uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
201 1.1 christos uj1 = u[j + 1]; /* for D3 only */
202 1.1 christos uj2 = u[j + 2]; /* for D3 only */
203 1.1 christos if (uj0 == v1) {
204 1.1 christos qhat = B;
205 1.1 christos rhat = uj1;
206 1.1 christos goto qhat_too_big;
207 1.1 christos } else {
208 1.1 christos u_int nn = COMBINE(uj0, uj1);
209 1.1 christos qhat = nn / v1;
210 1.1 christos rhat = nn % v1;
211 1.1 christos }
212 1.1 christos while (v2 * qhat > COMBINE(rhat, uj2)) {
213 1.1 christos qhat_too_big:
214 1.1 christos qhat--;
215 1.1 christos if ((rhat += v1) >= B)
216 1.1 christos break;
217 1.1 christos }
218 1.1 christos /*
219 1.1 christos * D4: Multiply and subtract.
220 1.1 christos * The variable `t' holds any borrows across the loop.
221 1.1 christos * We split this up so that we do not require v[0] = 0,
222 1.1 christos * and to eliminate a final special case.
223 1.1 christos */
224 1.1 christos for (t = 0, i = n; i > 0; i--) {
225 1.1 christos t = u[i + j] - v[i] * qhat - t;
226 1.1 christos u[i + j] = (digit)LHALF(t);
227 1.1 christos t = (B - HHALF(t)) & (B - 1);
228 1.1 christos }
229 1.1 christos t = u[j] - t;
230 1.1 christos u[j] = (digit)LHALF(t);
231 1.1 christos /*
232 1.1 christos * D5: test remainder.
233 1.1 christos * There is a borrow if and only if HHALF(t) is nonzero;
234 1.1 christos * in that (rare) case, qhat was too large (by exactly 1).
235 1.1 christos * Fix it by adding v[1..n] to u[j..j+n].
236 1.1 christos */
237 1.1 christos if (HHALF(t)) {
238 1.1 christos qhat--;
239 1.1 christos for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
240 1.1 christos t += u[i + j] + v[i];
241 1.1 christos u[i + j] = (digit)LHALF(t);
242 1.1 christos t = HHALF(t);
243 1.1 christos }
244 1.1 christos u[j] = (digit)LHALF(u[j] + t);
245 1.1 christos }
246 1.1 christos q[j] = (digit)qhat;
247 1.1 christos } while (++j <= m); /* D7: loop on j. */
248 1.1 christos
249 1.1 christos /*
250 1.1 christos * If caller wants the remainder, we have to calculate it as
251 1.1 christos * u[m..m+n] >> d (this is at most n digits and thus fits in
252 1.1 christos * u[m+1..m+n], but we may need more source digits).
253 1.1 christos */
254 1.1 christos if (arq) {
255 1.1 christos if (d) {
256 1.1 christos for (i = m + n; i > m; --i)
257 1.1 christos u[i] = (digit)(((u_int)u[i] >> d) |
258 1.1 christos LHALF((u_int)u[i - 1] << (HALF_BITS - d)));
259 1.1 christos u[i] = 0;
260 1.1 christos }
261 1.1 christos tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
262 1.1 christos tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
263 1.1 christos *arq = tmp.q;
264 1.1 christos }
265 1.1 christos
266 1.1 christos tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
267 1.1 christos tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
268 1.1 christos return (tmp.q);
269 1.1 christos }
270 1.1 christos
271 1.1 christos /*
272 1.1 christos * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
273 1.1 christos * `fall out' the left (there never will be any such anyway).
274 1.1 christos * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
275 1.1 christos */
276 1.1 christos static void
277 1.1 christos shl(digit *p, int len, int sh)
278 1.1 christos {
279 1.1 christos int i;
280 1.1 christos
281 1.1 christos for (i = 0; i < len; i++)
282 1.1 christos p[i] = (digit)(LHALF((u_int)p[i] << sh) |
283 1.1 christos ((u_int)p[i + 1] >> (HALF_BITS - sh)));
284 1.1 christos p[i] = (digit)(LHALF((u_int)p[i] << sh));
285 1.1 christos }
286