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