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