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