xref: /netbsd-src/common/lib/libc/quad/qdivrem.c (revision 567219e1d7461bff1b180e494a9674a287b057a7)
1*567219e1Smatt /*	$NetBSD: qdivrem.c,v 1.4 2012/03/20 16:21:41 matt Exp $	*/
237c9f0a6Schristos 
337c9f0a6Schristos /*-
437c9f0a6Schristos  * Copyright (c) 1992, 1993
537c9f0a6Schristos  *	The Regents of the University of California.  All rights reserved.
637c9f0a6Schristos  *
737c9f0a6Schristos  * This software was developed by the Computer Systems Engineering group
837c9f0a6Schristos  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
937c9f0a6Schristos  * contributed to Berkeley.
1037c9f0a6Schristos  *
1137c9f0a6Schristos  * Redistribution and use in source and binary forms, with or without
1237c9f0a6Schristos  * modification, are permitted provided that the following conditions
1337c9f0a6Schristos  * are met:
1437c9f0a6Schristos  * 1. Redistributions of source code must retain the above copyright
1537c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer.
1637c9f0a6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1737c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer in the
1837c9f0a6Schristos  *    documentation and/or other materials provided with the distribution.
1937c9f0a6Schristos  * 3. Neither the name of the University nor the names of its contributors
2037c9f0a6Schristos  *    may be used to endorse or promote products derived from this software
2137c9f0a6Schristos  *    without specific prior written permission.
2237c9f0a6Schristos  *
2337c9f0a6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2437c9f0a6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2537c9f0a6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2637c9f0a6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2737c9f0a6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2837c9f0a6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2937c9f0a6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3037c9f0a6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3137c9f0a6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3237c9f0a6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3337c9f0a6Schristos  * SUCH DAMAGE.
3437c9f0a6Schristos  */
3537c9f0a6Schristos 
3637c9f0a6Schristos #include <sys/cdefs.h>
3737c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
3837c9f0a6Schristos #if 0
3937c9f0a6Schristos static char sccsid[] = "@(#)qdivrem.c	8.1 (Berkeley) 6/4/93";
4037c9f0a6Schristos #else
41*567219e1Smatt __RCSID("$NetBSD: qdivrem.c,v 1.4 2012/03/20 16:21:41 matt Exp $");
4237c9f0a6Schristos #endif
4337c9f0a6Schristos #endif /* LIBC_SCCS and not lint */
4437c9f0a6Schristos 
4537c9f0a6Schristos /*
4637c9f0a6Schristos  * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
4737c9f0a6Schristos  * section 4.3.1, pp. 257--259.
4837c9f0a6Schristos  */
4937c9f0a6Schristos 
5037c9f0a6Schristos #include "quad.h"
5137c9f0a6Schristos 
52a5fd370aSchristos #define	B	((int)1 << (unsigned int)HALF_BITS)	/* digit base */
5337c9f0a6Schristos 
5437c9f0a6Schristos /* Combine two `digits' to make a single two-digit number. */
55a5fd370aSchristos #define	COMBINE(a, b) (((u_int)(a) << (unsigned int)HALF_BITS) | (b))
5637c9f0a6Schristos 
5737c9f0a6Schristos /* select a type for digits in base B: use unsigned short if they fit */
5837c9f0a6Schristos #if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff
5937c9f0a6Schristos typedef unsigned short digit;
6037c9f0a6Schristos #else
6137c9f0a6Schristos typedef u_int digit;
6237c9f0a6Schristos #endif
6337c9f0a6Schristos 
64*567219e1Smatt static void shl(digit *p, int len, int sh);
6537c9f0a6Schristos 
6637c9f0a6Schristos /*
6737c9f0a6Schristos  * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
6837c9f0a6Schristos  *
6937c9f0a6Schristos  * We do this in base 2-sup-HALF_BITS, so that all intermediate products
7037c9f0a6Schristos  * fit within u_int.  As a consequence, the maximum length dividend and
7137c9f0a6Schristos  * divisor are 4 `digits' in this base (they are shorter if they have
7237c9f0a6Schristos  * leading zeros).
7337c9f0a6Schristos  */
7437c9f0a6Schristos u_quad_t
__qdivrem(u_quad_t uq,u_quad_t vq,u_quad_t * arq)75103c3602Scegger __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)
7637c9f0a6Schristos {
7737c9f0a6Schristos 	union uu tmp;
7837c9f0a6Schristos 	digit *u, *v, *q;
7937c9f0a6Schristos 	digit v1, v2;
8037c9f0a6Schristos 	u_int qhat, rhat, t;
8137c9f0a6Schristos 	int m, n, d, j, i;
8237c9f0a6Schristos 	digit uspace[5], vspace[5], qspace[5];
8337c9f0a6Schristos 
8437c9f0a6Schristos 	/*
8537c9f0a6Schristos 	 * Take care of special cases: divide by zero, and u < v.
8637c9f0a6Schristos 	 */
8737c9f0a6Schristos 	if (vq == 0) {
8837c9f0a6Schristos 		/* divide by zero. */
8937c9f0a6Schristos 		static volatile const unsigned int zero = 0;
9037c9f0a6Schristos 
9137c9f0a6Schristos 		tmp.ul[H] = tmp.ul[L] = 1 / zero;
9237c9f0a6Schristos 		if (arq)
9337c9f0a6Schristos 			*arq = uq;
9437c9f0a6Schristos 		return (tmp.q);
9537c9f0a6Schristos 	}
9637c9f0a6Schristos 	if (uq < vq) {
9737c9f0a6Schristos 		if (arq)
9837c9f0a6Schristos 			*arq = uq;
9937c9f0a6Schristos 		return (0);
10037c9f0a6Schristos 	}
10137c9f0a6Schristos 	u = &uspace[0];
10237c9f0a6Schristos 	v = &vspace[0];
10337c9f0a6Schristos 	q = &qspace[0];
10437c9f0a6Schristos 
10537c9f0a6Schristos 	/*
10637c9f0a6Schristos 	 * Break dividend and divisor into digits in base B, then
10737c9f0a6Schristos 	 * count leading zeros to determine m and n.  When done, we
10837c9f0a6Schristos 	 * will have:
10937c9f0a6Schristos 	 *	u = (u[1]u[2]...u[m+n]) sub B
11037c9f0a6Schristos 	 *	v = (v[1]v[2]...v[n]) sub B
11137c9f0a6Schristos 	 *	v[1] != 0
11237c9f0a6Schristos 	 *	1 < n <= 4 (if n = 1, we use a different division algorithm)
11337c9f0a6Schristos 	 *	m >= 0 (otherwise u < v, which we already checked)
11437c9f0a6Schristos 	 *	m + n = 4
11537c9f0a6Schristos 	 * and thus
11637c9f0a6Schristos 	 *	m = 4 - n <= 2
11737c9f0a6Schristos 	 */
11837c9f0a6Schristos 	tmp.uq = uq;
11937c9f0a6Schristos 	u[0] = 0;
12037c9f0a6Schristos 	u[1] = (digit)HHALF(tmp.ul[H]);
12137c9f0a6Schristos 	u[2] = (digit)LHALF(tmp.ul[H]);
12237c9f0a6Schristos 	u[3] = (digit)HHALF(tmp.ul[L]);
12337c9f0a6Schristos 	u[4] = (digit)LHALF(tmp.ul[L]);
12437c9f0a6Schristos 	tmp.uq = vq;
12537c9f0a6Schristos 	v[1] = (digit)HHALF(tmp.ul[H]);
12637c9f0a6Schristos 	v[2] = (digit)LHALF(tmp.ul[H]);
12737c9f0a6Schristos 	v[3] = (digit)HHALF(tmp.ul[L]);
12837c9f0a6Schristos 	v[4] = (digit)LHALF(tmp.ul[L]);
12937c9f0a6Schristos 	for (n = 4; v[1] == 0; v++) {
13037c9f0a6Schristos 		if (--n == 1) {
13137c9f0a6Schristos 			u_int rbj;	/* r*B+u[j] (not root boy jim) */
13237c9f0a6Schristos 			digit q1, q2, q3, q4;
13337c9f0a6Schristos 
13437c9f0a6Schristos 			/*
13537c9f0a6Schristos 			 * Change of plan, per exercise 16.
13637c9f0a6Schristos 			 *	r = 0;
13737c9f0a6Schristos 			 *	for j = 1..4:
13837c9f0a6Schristos 			 *		q[j] = floor((r*B + u[j]) / v),
13937c9f0a6Schristos 			 *		r = (r*B + u[j]) % v;
14037c9f0a6Schristos 			 * We unroll this completely here.
14137c9f0a6Schristos 			 */
14237c9f0a6Schristos 			t = v[2];	/* nonzero, by definition */
14337c9f0a6Schristos 			q1 = (digit)(u[1] / t);
14437c9f0a6Schristos 			rbj = COMBINE(u[1] % t, u[2]);
14537c9f0a6Schristos 			q2 = (digit)(rbj / t);
14637c9f0a6Schristos 			rbj = COMBINE(rbj % t, u[3]);
14737c9f0a6Schristos 			q3 = (digit)(rbj / t);
14837c9f0a6Schristos 			rbj = COMBINE(rbj % t, u[4]);
14937c9f0a6Schristos 			q4 = (digit)(rbj / t);
15037c9f0a6Schristos 			if (arq)
15137c9f0a6Schristos 				*arq = rbj % t;
15237c9f0a6Schristos 			tmp.ul[H] = COMBINE(q1, q2);
15337c9f0a6Schristos 			tmp.ul[L] = COMBINE(q3, q4);
15437c9f0a6Schristos 			return (tmp.q);
15537c9f0a6Schristos 		}
15637c9f0a6Schristos 	}
15737c9f0a6Schristos 
15837c9f0a6Schristos 	/*
15937c9f0a6Schristos 	 * By adjusting q once we determine m, we can guarantee that
16037c9f0a6Schristos 	 * there is a complete four-digit quotient at &qspace[1] when
16137c9f0a6Schristos 	 * we finally stop.
16237c9f0a6Schristos 	 */
16337c9f0a6Schristos 	for (m = 4 - n; u[1] == 0; u++)
16437c9f0a6Schristos 		m--;
16537c9f0a6Schristos 	for (i = 4 - m; --i >= 0;)
16637c9f0a6Schristos 		q[i] = 0;
16737c9f0a6Schristos 	q += 4 - m;
16837c9f0a6Schristos 
16937c9f0a6Schristos 	/*
17037c9f0a6Schristos 	 * Here we run Program D, translated from MIX to C and acquiring
17137c9f0a6Schristos 	 * a few minor changes.
17237c9f0a6Schristos 	 *
17337c9f0a6Schristos 	 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
17437c9f0a6Schristos 	 */
17537c9f0a6Schristos 	d = 0;
176a5fd370aSchristos 	for (t = v[1]; t < B / 2; t <<= (unsigned int)1)
17737c9f0a6Schristos 		d++;
17837c9f0a6Schristos 	if (d > 0) {
17937c9f0a6Schristos 		shl(&u[0], m + n, d);		/* u <<= d */
18037c9f0a6Schristos 		shl(&v[1], n - 1, d);		/* v <<= d */
18137c9f0a6Schristos 	}
18237c9f0a6Schristos 	/*
18337c9f0a6Schristos 	 * D2: j = 0.
18437c9f0a6Schristos 	 */
18537c9f0a6Schristos 	j = 0;
18637c9f0a6Schristos 	v1 = v[1];	/* for D3 -- note that v[1..n] are constant */
18737c9f0a6Schristos 	v2 = v[2];	/* for D3 */
18837c9f0a6Schristos 	do {
18937c9f0a6Schristos 		digit uj0, uj1, uj2;
19037c9f0a6Schristos 
19137c9f0a6Schristos 		/*
19237c9f0a6Schristos 		 * D3: Calculate qhat (\^q, in TeX notation).
19337c9f0a6Schristos 		 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
19437c9f0a6Schristos 		 * let rhat = (u[j]*B + u[j+1]) mod v[1].
19537c9f0a6Schristos 		 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
19637c9f0a6Schristos 		 * decrement qhat and increase rhat correspondingly.
19737c9f0a6Schristos 		 * Note that if rhat >= B, v[2]*qhat < rhat*B.
19837c9f0a6Schristos 		 */
19937c9f0a6Schristos 		uj0 = u[j + 0];	/* for D3 only -- note that u[j+...] change */
20037c9f0a6Schristos 		uj1 = u[j + 1];	/* for D3 only */
20137c9f0a6Schristos 		uj2 = u[j + 2];	/* for D3 only */
20237c9f0a6Schristos 		if (uj0 == v1) {
20337c9f0a6Schristos 			qhat = B;
20437c9f0a6Schristos 			rhat = uj1;
20537c9f0a6Schristos 			goto qhat_too_big;
20637c9f0a6Schristos 		} else {
20737c9f0a6Schristos 			u_int nn = COMBINE(uj0, uj1);
20837c9f0a6Schristos 			qhat = nn / v1;
20937c9f0a6Schristos 			rhat = nn % v1;
21037c9f0a6Schristos 		}
21137c9f0a6Schristos 		while (v2 * qhat > COMBINE(rhat, uj2)) {
21237c9f0a6Schristos 	qhat_too_big:
21337c9f0a6Schristos 			qhat--;
21437c9f0a6Schristos 			if ((rhat += v1) >= B)
21537c9f0a6Schristos 				break;
21637c9f0a6Schristos 		}
21737c9f0a6Schristos 		/*
21837c9f0a6Schristos 		 * D4: Multiply and subtract.
21937c9f0a6Schristos 		 * The variable `t' holds any borrows across the loop.
22037c9f0a6Schristos 		 * We split this up so that we do not require v[0] = 0,
22137c9f0a6Schristos 		 * and to eliminate a final special case.
22237c9f0a6Schristos 		 */
22337c9f0a6Schristos 		for (t = 0, i = n; i > 0; i--) {
22437c9f0a6Schristos 			t = u[i + j] - v[i] * qhat - t;
22537c9f0a6Schristos 			u[i + j] = (digit)LHALF(t);
22637c9f0a6Schristos 			t = (B - HHALF(t)) & (B - 1);
22737c9f0a6Schristos 		}
22837c9f0a6Schristos 		t = u[j] - t;
22937c9f0a6Schristos 		u[j] = (digit)LHALF(t);
23037c9f0a6Schristos 		/*
23137c9f0a6Schristos 		 * D5: test remainder.
23237c9f0a6Schristos 		 * There is a borrow if and only if HHALF(t) is nonzero;
23337c9f0a6Schristos 		 * in that (rare) case, qhat was too large (by exactly 1).
23437c9f0a6Schristos 		 * Fix it by adding v[1..n] to u[j..j+n].
23537c9f0a6Schristos 		 */
23637c9f0a6Schristos 		if (HHALF(t)) {
23737c9f0a6Schristos 			qhat--;
23837c9f0a6Schristos 			for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
23937c9f0a6Schristos 				t += u[i + j] + v[i];
24037c9f0a6Schristos 				u[i + j] = (digit)LHALF(t);
24137c9f0a6Schristos 				t = HHALF(t);
24237c9f0a6Schristos 			}
24337c9f0a6Schristos 			u[j] = (digit)LHALF(u[j] + t);
24437c9f0a6Schristos 		}
24537c9f0a6Schristos 		q[j] = (digit)qhat;
24637c9f0a6Schristos 	} while (++j <= m);		/* D7: loop on j. */
24737c9f0a6Schristos 
24837c9f0a6Schristos 	/*
24937c9f0a6Schristos 	 * If caller wants the remainder, we have to calculate it as
25037c9f0a6Schristos 	 * u[m..m+n] >> d (this is at most n digits and thus fits in
25137c9f0a6Schristos 	 * u[m+1..m+n], but we may need more source digits).
25237c9f0a6Schristos 	 */
25337c9f0a6Schristos 	if (arq) {
25437c9f0a6Schristos 		if (d) {
25537c9f0a6Schristos 			for (i = m + n; i > m; --i)
25637c9f0a6Schristos 				u[i] = (digit)(((u_int)u[i] >> d) |
257a5fd370aSchristos 				    LHALF((u_int)u[i - 1] << (unsigned int)(HALF_BITS - d)));
25837c9f0a6Schristos 			u[i] = 0;
25937c9f0a6Schristos 		}
26037c9f0a6Schristos 		tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
26137c9f0a6Schristos 		tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
26237c9f0a6Schristos 		*arq = tmp.q;
26337c9f0a6Schristos 	}
26437c9f0a6Schristos 
26537c9f0a6Schristos 	tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
26637c9f0a6Schristos 	tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
26737c9f0a6Schristos 	return (tmp.q);
26837c9f0a6Schristos }
26937c9f0a6Schristos 
27037c9f0a6Schristos /*
27137c9f0a6Schristos  * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
27237c9f0a6Schristos  * `fall out' the left (there never will be any such anyway).
27337c9f0a6Schristos  * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
27437c9f0a6Schristos  */
27537c9f0a6Schristos static void
shl(digit * p,int len,int sh)27637c9f0a6Schristos shl(digit *p, int len, int sh)
27737c9f0a6Schristos {
27837c9f0a6Schristos 	int i;
27937c9f0a6Schristos 
28037c9f0a6Schristos 	for (i = 0; i < len; i++)
28137c9f0a6Schristos 		p[i] = (digit)(LHALF((u_int)p[i] << sh) |
28237c9f0a6Schristos 		    ((u_int)p[i + 1] >> (HALF_BITS - sh)));
28337c9f0a6Schristos 	p[i] = (digit)(LHALF((u_int)p[i] << sh));
28437c9f0a6Schristos }
285