153430Sbostic /*-
2*61160Sbostic * Copyright (c) 1992, 1993
3*61160Sbostic * The Regents of the University of California. All rights reserved.
453430Sbostic *
553794Sbostic * This software was developed by the Computer Systems Engineering group
653794Sbostic * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
753794Sbostic * contributed to Berkeley.
853794Sbostic *
953430Sbostic * %sccs.include.redist.c%
1053430Sbostic */
1153430Sbostic
1253430Sbostic #if defined(LIBC_SCCS) && !defined(lint)
13*61160Sbostic static char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 06/04/93";
1453430Sbostic #endif /* LIBC_SCCS and not lint */
1553430Sbostic
1653794Sbostic /*
1753794Sbostic * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
1853794Sbostic * section 4.3.1, pp. 257--259.
1953794Sbostic */
2053459Sbostic
2153794Sbostic #include "quad.h"
2253459Sbostic
2353794Sbostic #define B (1 << HALF_BITS) /* digit base */
2453459Sbostic
2553794Sbostic /* Combine two `digits' to make a single two-digit number. */
2653794Sbostic #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
2753459Sbostic
2853794Sbostic /* select a type for digits in base B: use unsigned short if they fit */
2953794Sbostic #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
3053794Sbostic typedef unsigned short digit;
3153455Sbostic #else
3253794Sbostic typedef u_long digit;
3353455Sbostic #endif
3451748Smckusick
3553794Sbostic /*
3653794Sbostic * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
3753794Sbostic * `fall out' the left (there never will be any such anyway).
3853794Sbostic * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
3953794Sbostic */
4053794Sbostic static void
shl(register digit * p,register int len,register int sh)4153794Sbostic shl(register digit *p, register int len, register int sh)
4253794Sbostic {
4353794Sbostic register int i;
4451748Smckusick
4553794Sbostic for (i = 0; i < len; i++)
4653794Sbostic p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
4753794Sbostic p[i] = LHALF(p[i] << sh);
4853794Sbostic }
4951748Smckusick
5053794Sbostic /*
5153794Sbostic * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
5253794Sbostic *
5353794Sbostic * We do this in base 2-sup-HALF_BITS, so that all intermediate products
5453794Sbostic * fit within u_long. As a consequence, the maximum length dividend and
5553794Sbostic * divisor are 4 `digits' in this base (they are shorter if they have
5653794Sbostic * leading zeros).
5753794Sbostic */
5854431Sbostic u_quad_t
__qdivrem(uq,vq,arq)5954431Sbostic __qdivrem(uq, vq, arq)
6054431Sbostic u_quad_t uq, vq, *arq;
6153794Sbostic {
6253794Sbostic union uu tmp;
6353794Sbostic digit *u, *v, *q;
6453794Sbostic register digit v1, v2;
6553794Sbostic u_long qhat, rhat, t;
6653794Sbostic int m, n, d, j, i;
6753794Sbostic digit uspace[5], vspace[5], qspace[5];
6851748Smckusick
6953794Sbostic /*
7053794Sbostic * Take care of special cases: divide by zero, and u < v.
7153794Sbostic */
7253794Sbostic if (vq == 0) {
7353794Sbostic /* divide by zero. */
7453794Sbostic static volatile const unsigned int zero = 0;
7551748Smckusick
7653794Sbostic tmp.ul[H] = tmp.ul[L] = 1 / zero;
7753794Sbostic if (arq)
7853794Sbostic *arq = uq;
7953794Sbostic return (tmp.q);
8051748Smckusick }
8153794Sbostic if (uq < vq) {
8253794Sbostic if (arq)
8353794Sbostic *arq = uq;
8453794Sbostic return (0);
8553794Sbostic }
8653794Sbostic u = &uspace[0];
8753794Sbostic v = &vspace[0];
8853794Sbostic q = &qspace[0];
8951748Smckusick
9053794Sbostic /*
9153794Sbostic * Break dividend and divisor into digits in base B, then
9253794Sbostic * count leading zeros to determine m and n. When done, we
9353794Sbostic * will have:
9453794Sbostic * u = (u[1]u[2]...u[m+n]) sub B
9553794Sbostic * v = (v[1]v[2]...v[n]) sub B
9653794Sbostic * v[1] != 0
9753794Sbostic * 1 < n <= 4 (if n = 1, we use a different division algorithm)
9853794Sbostic * m >= 0 (otherwise u < v, which we already checked)
9953794Sbostic * m + n = 4
10053794Sbostic * and thus
10153794Sbostic * m = 4 - n <= 2
10253794Sbostic */
10353794Sbostic tmp.uq = uq;
10453794Sbostic u[0] = 0;
10553794Sbostic u[1] = HHALF(tmp.ul[H]);
10653794Sbostic u[2] = LHALF(tmp.ul[H]);
10753794Sbostic u[3] = HHALF(tmp.ul[L]);
10853794Sbostic u[4] = LHALF(tmp.ul[L]);
10953794Sbostic tmp.uq = vq;
11053794Sbostic v[1] = HHALF(tmp.ul[H]);
11153794Sbostic v[2] = LHALF(tmp.ul[H]);
11253794Sbostic v[3] = HHALF(tmp.ul[L]);
11353794Sbostic v[4] = LHALF(tmp.ul[L]);
11453794Sbostic for (n = 4; v[1] == 0; v++) {
11553794Sbostic if (--n == 1) {
11653794Sbostic u_long rbj; /* r*B+u[j] (not root boy jim) */
11753794Sbostic digit q1, q2, q3, q4;
11851748Smckusick
11953794Sbostic /*
12053794Sbostic * Change of plan, per exercise 16.
12153794Sbostic * r = 0;
12253794Sbostic * for j = 1..4:
12353794Sbostic * q[j] = floor((r*B + u[j]) / v),
12453794Sbostic * r = (r*B + u[j]) % v;
12553794Sbostic * We unroll this completely here.
12653794Sbostic */
12753794Sbostic t = v[2]; /* nonzero, by definition */
12853794Sbostic q1 = u[1] / t;
12953794Sbostic rbj = COMBINE(u[1] % t, u[2]);
13053794Sbostic q2 = rbj / t;
13153794Sbostic rbj = COMBINE(rbj % t, u[3]);
13253794Sbostic q3 = rbj / t;
13353794Sbostic rbj = COMBINE(rbj % t, u[4]);
13453794Sbostic q4 = rbj / t;
13553794Sbostic if (arq)
13653794Sbostic *arq = rbj % t;
13753794Sbostic tmp.ul[H] = COMBINE(q1, q2);
13853794Sbostic tmp.ul[L] = COMBINE(q3, q4);
13953794Sbostic return (tmp.q);
14053794Sbostic }
14151748Smckusick }
14251748Smckusick
14353794Sbostic /*
14453794Sbostic * By adjusting q once we determine m, we can guarantee that
14553794Sbostic * there is a complete four-digit quotient at &qspace[1] when
14653794Sbostic * we finally stop.
14753794Sbostic */
14853794Sbostic for (m = 4 - n; u[1] == 0; u++)
14953794Sbostic m--;
15053794Sbostic for (i = 4 - m; --i >= 0;)
15153794Sbostic q[i] = 0;
15253794Sbostic q += 4 - m;
15351748Smckusick
15453794Sbostic /*
15553794Sbostic * Here we run Program D, translated from MIX to C and acquiring
15653794Sbostic * a few minor changes.
15753794Sbostic *
15853794Sbostic * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
15953794Sbostic */
16053794Sbostic d = 0;
16153794Sbostic for (t = v[1]; t < B / 2; t <<= 1)
16253794Sbostic d++;
16353794Sbostic if (d > 0) {
16453794Sbostic shl(&u[0], m + n, d); /* u <<= d */
16553794Sbostic shl(&v[1], n - 1, d); /* v <<= d */
16651748Smckusick }
16753794Sbostic /*
16853794Sbostic * D2: j = 0.
16953794Sbostic */
17053794Sbostic j = 0;
17153794Sbostic v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
17253794Sbostic v2 = v[2]; /* for D3 */
17353794Sbostic do {
17453794Sbostic register digit uj0, uj1, uj2;
17553794Sbostic
17653794Sbostic /*
17753794Sbostic * D3: Calculate qhat (\^q, in TeX notation).
17853794Sbostic * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
17953794Sbostic * let rhat = (u[j]*B + u[j+1]) mod v[1].
18053794Sbostic * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
18153794Sbostic * decrement qhat and increase rhat correspondingly.
18253794Sbostic * Note that if rhat >= B, v[2]*qhat < rhat*B.
18353794Sbostic */
18453794Sbostic uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
18553794Sbostic uj1 = u[j + 1]; /* for D3 only */
18653794Sbostic uj2 = u[j + 2]; /* for D3 only */
18753794Sbostic if (uj0 == v1) {
18853794Sbostic qhat = B;
18953794Sbostic rhat = uj1;
19053794Sbostic goto qhat_too_big;
19153794Sbostic } else {
19253794Sbostic u_long n = COMBINE(uj0, uj1);
19353794Sbostic qhat = n / v1;
19453794Sbostic rhat = n % v1;
19553794Sbostic }
19653794Sbostic while (v2 * qhat > COMBINE(rhat, uj2)) {
19753794Sbostic qhat_too_big:
19853794Sbostic qhat--;
19953794Sbostic if ((rhat += v1) >= B)
20053794Sbostic break;
20153794Sbostic }
20253794Sbostic /*
20353794Sbostic * D4: Multiply and subtract.
20453794Sbostic * The variable `t' holds any borrows across the loop.
20553794Sbostic * We split this up so that we do not require v[0] = 0,
20653794Sbostic * and to eliminate a final special case.
20753794Sbostic */
20853794Sbostic for (t = 0, i = n; i > 0; i--) {
20953794Sbostic t = u[i + j] - v[i] * qhat - t;
21053794Sbostic u[i + j] = LHALF(t);
21153794Sbostic t = (B - HHALF(t)) & (B - 1);
21253794Sbostic }
21353794Sbostic t = u[j] - t;
21453794Sbostic u[j] = LHALF(t);
21553794Sbostic /*
21653794Sbostic * D5: test remainder.
21753794Sbostic * There is a borrow if and only if HHALF(t) is nonzero;
21853794Sbostic * in that (rare) case, qhat was too large (by exactly 1).
21953794Sbostic * Fix it by adding v[1..n] to u[j..j+n].
22053794Sbostic */
22153794Sbostic if (HHALF(t)) {
22253794Sbostic qhat--;
22353794Sbostic for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
22453794Sbostic t += u[i + j] + v[i];
22553794Sbostic u[i + j] = LHALF(t);
22653794Sbostic t = HHALF(t);
22753794Sbostic }
22853794Sbostic u[j] = LHALF(u[j] + t);
22953794Sbostic }
23053794Sbostic q[j] = qhat;
23153794Sbostic } while (++j <= m); /* D7: loop on j. */
23251748Smckusick
23353794Sbostic /*
23453794Sbostic * If caller wants the remainder, we have to calculate it as
23553794Sbostic * u[m..m+n] >> d (this is at most n digits and thus fits in
23653794Sbostic * u[m+1..m+n], but we may need more source digits).
23753794Sbostic */
23853794Sbostic if (arq) {
23953794Sbostic if (d) {
24053794Sbostic for (i = m + n; i > m; --i)
24153794Sbostic u[i] = (u[i] >> d) |
24253794Sbostic LHALF(u[i - 1] << (HALF_BITS - d));
24353794Sbostic u[i] = 0;
24453794Sbostic }
24553794Sbostic tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
24653794Sbostic tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
24753794Sbostic *arq = tmp.q;
24851748Smckusick }
24951748Smckusick
25053794Sbostic tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
25153794Sbostic tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
25253794Sbostic return (tmp.q);
25351748Smckusick }
254