158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1992, 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * This software was developed by the Computer Systems Engineering group
858f0484fSRodney W. Grimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
958f0484fSRodney W. Grimes * contributed to Berkeley.
1058f0484fSRodney W. Grimes *
1158f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes * are met:
1458f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes * without specific prior written permission.
2258f0484fSRodney W. Grimes *
2358f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes * SUCH DAMAGE.
3458f0484fSRodney W. Grimes */
3558f0484fSRodney W. Grimes
3658f0484fSRodney W. Grimes /*
3758f0484fSRodney W. Grimes * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
3858f0484fSRodney W. Grimes * section 4.3.1, pp. 257--259.
3958f0484fSRodney W. Grimes */
4058f0484fSRodney W. Grimes
4158f0484fSRodney W. Grimes #include "quad.h"
4258f0484fSRodney W. Grimes
43feb1d550SDimitry Andric #define B (1L << HALF_BITS) /* digit base */
4458f0484fSRodney W. Grimes
4558f0484fSRodney W. Grimes /* Combine two `digits' to make a single two-digit number. */
4658f0484fSRodney W. Grimes #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
4758f0484fSRodney W. Grimes
4858f0484fSRodney W. Grimes /* select a type for digits in base B: use unsigned short if they fit */
4958f0484fSRodney W. Grimes #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
5058f0484fSRodney W. Grimes typedef unsigned short digit;
5158f0484fSRodney W. Grimes #else
5258f0484fSRodney W. Grimes typedef u_long digit;
5358f0484fSRodney W. Grimes #endif
5458f0484fSRodney W. Grimes
5558f0484fSRodney W. Grimes /*
5658f0484fSRodney W. Grimes * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
5758f0484fSRodney W. Grimes * `fall out' the left (there never will be any such anyway).
5858f0484fSRodney W. Grimes * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
5958f0484fSRodney W. Grimes */
6058f0484fSRodney W. Grimes static void
shl(digit * p,int len,int sh)618fb3f3f6SDavid E. O'Brien shl(digit *p, int len, int sh)
6258f0484fSRodney W. Grimes {
638fb3f3f6SDavid E. O'Brien int i;
6458f0484fSRodney W. Grimes
6558f0484fSRodney W. Grimes for (i = 0; i < len; i++)
6658f0484fSRodney W. Grimes p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
6758f0484fSRodney W. Grimes p[i] = LHALF(p[i] << sh);
6858f0484fSRodney W. Grimes }
6958f0484fSRodney W. Grimes
7058f0484fSRodney W. Grimes /*
7158f0484fSRodney W. Grimes * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
7258f0484fSRodney W. Grimes *
7358f0484fSRodney W. Grimes * We do this in base 2-sup-HALF_BITS, so that all intermediate products
7458f0484fSRodney W. Grimes * fit within u_long. As a consequence, the maximum length dividend and
7558f0484fSRodney W. Grimes * divisor are 4 `digits' in this base (they are shorter if they have
7658f0484fSRodney W. Grimes * leading zeros).
7758f0484fSRodney W. Grimes */
7858f0484fSRodney W. Grimes u_quad_t
__qdivrem(u_quad_t uq,u_quad_t vq,u_quad_t * arq)796492be46SEd Maste __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)
8058f0484fSRodney W. Grimes {
8158f0484fSRodney W. Grimes union uu tmp;
8258f0484fSRodney W. Grimes digit *u, *v, *q;
838fb3f3f6SDavid E. O'Brien digit v1, v2;
8458f0484fSRodney W. Grimes u_long qhat, rhat, t;
8558f0484fSRodney W. Grimes int m, n, d, j, i;
8658f0484fSRodney W. Grimes digit uspace[5], vspace[5], qspace[5];
8758f0484fSRodney W. Grimes
8858f0484fSRodney W. Grimes /*
8958f0484fSRodney W. Grimes * Take care of special cases: divide by zero, and u < v.
9058f0484fSRodney W. Grimes */
91*1024bb26SHans Petter Selasky if (__predict_false(vq == 0)) {
9258f0484fSRodney W. Grimes /* divide by zero. */
9358f0484fSRodney W. Grimes static volatile const unsigned int zero = 0;
9458f0484fSRodney W. Grimes
9558f0484fSRodney W. Grimes tmp.ul[H] = tmp.ul[L] = 1 / zero;
9658f0484fSRodney W. Grimes if (arq)
9758f0484fSRodney W. Grimes *arq = uq;
9858f0484fSRodney W. Grimes return (tmp.q);
9958f0484fSRodney W. Grimes }
10058f0484fSRodney W. Grimes if (uq < vq) {
10158f0484fSRodney W. Grimes if (arq)
10258f0484fSRodney W. Grimes *arq = uq;
10358f0484fSRodney W. Grimes return (0);
10458f0484fSRodney W. Grimes }
10558f0484fSRodney W. Grimes u = &uspace[0];
10658f0484fSRodney W. Grimes v = &vspace[0];
10758f0484fSRodney W. Grimes q = &qspace[0];
10858f0484fSRodney W. Grimes
10958f0484fSRodney W. Grimes /*
11058f0484fSRodney W. Grimes * Break dividend and divisor into digits in base B, then
11158f0484fSRodney W. Grimes * count leading zeros to determine m and n. When done, we
11258f0484fSRodney W. Grimes * will have:
11358f0484fSRodney W. Grimes * u = (u[1]u[2]...u[m+n]) sub B
11458f0484fSRodney W. Grimes * v = (v[1]v[2]...v[n]) sub B
11558f0484fSRodney W. Grimes * v[1] != 0
11658f0484fSRodney W. Grimes * 1 < n <= 4 (if n = 1, we use a different division algorithm)
11758f0484fSRodney W. Grimes * m >= 0 (otherwise u < v, which we already checked)
11858f0484fSRodney W. Grimes * m + n = 4
11958f0484fSRodney W. Grimes * and thus
12058f0484fSRodney W. Grimes * m = 4 - n <= 2
12158f0484fSRodney W. Grimes */
12258f0484fSRodney W. Grimes tmp.uq = uq;
12358f0484fSRodney W. Grimes u[0] = 0;
12458f0484fSRodney W. Grimes u[1] = HHALF(tmp.ul[H]);
12558f0484fSRodney W. Grimes u[2] = LHALF(tmp.ul[H]);
12658f0484fSRodney W. Grimes u[3] = HHALF(tmp.ul[L]);
12758f0484fSRodney W. Grimes u[4] = LHALF(tmp.ul[L]);
12858f0484fSRodney W. Grimes tmp.uq = vq;
12958f0484fSRodney W. Grimes v[1] = HHALF(tmp.ul[H]);
13058f0484fSRodney W. Grimes v[2] = LHALF(tmp.ul[H]);
13158f0484fSRodney W. Grimes v[3] = HHALF(tmp.ul[L]);
13258f0484fSRodney W. Grimes v[4] = LHALF(tmp.ul[L]);
13358f0484fSRodney W. Grimes for (n = 4; v[1] == 0; v++) {
13458f0484fSRodney W. Grimes if (--n == 1) {
13558f0484fSRodney W. Grimes u_long rbj; /* r*B+u[j] (not root boy jim) */
13658f0484fSRodney W. Grimes digit q1, q2, q3, q4;
13758f0484fSRodney W. Grimes
13858f0484fSRodney W. Grimes /*
13958f0484fSRodney W. Grimes * Change of plan, per exercise 16.
14058f0484fSRodney W. Grimes * r = 0;
14158f0484fSRodney W. Grimes * for j = 1..4:
14258f0484fSRodney W. Grimes * q[j] = floor((r*B + u[j]) / v),
14358f0484fSRodney W. Grimes * r = (r*B + u[j]) % v;
14458f0484fSRodney W. Grimes * We unroll this completely here.
14558f0484fSRodney W. Grimes */
14658f0484fSRodney W. Grimes t = v[2]; /* nonzero, by definition */
14758f0484fSRodney W. Grimes q1 = u[1] / t;
14858f0484fSRodney W. Grimes rbj = COMBINE(u[1] % t, u[2]);
14958f0484fSRodney W. Grimes q2 = rbj / t;
15058f0484fSRodney W. Grimes rbj = COMBINE(rbj % t, u[3]);
15158f0484fSRodney W. Grimes q3 = rbj / t;
15258f0484fSRodney W. Grimes rbj = COMBINE(rbj % t, u[4]);
15358f0484fSRodney W. Grimes q4 = rbj / t;
15458f0484fSRodney W. Grimes if (arq)
15558f0484fSRodney W. Grimes *arq = rbj % t;
15658f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(q1, q2);
15758f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(q3, q4);
15858f0484fSRodney W. Grimes return (tmp.q);
15958f0484fSRodney W. Grimes }
16058f0484fSRodney W. Grimes }
16158f0484fSRodney W. Grimes
16258f0484fSRodney W. Grimes /*
16358f0484fSRodney W. Grimes * By adjusting q once we determine m, we can guarantee that
16458f0484fSRodney W. Grimes * there is a complete four-digit quotient at &qspace[1] when
16558f0484fSRodney W. Grimes * we finally stop.
16658f0484fSRodney W. Grimes */
16758f0484fSRodney W. Grimes for (m = 4 - n; u[1] == 0; u++)
16858f0484fSRodney W. Grimes m--;
16958f0484fSRodney W. Grimes for (i = 4 - m; --i >= 0;)
17058f0484fSRodney W. Grimes q[i] = 0;
17158f0484fSRodney W. Grimes q += 4 - m;
17258f0484fSRodney W. Grimes
17358f0484fSRodney W. Grimes /*
17458f0484fSRodney W. Grimes * Here we run Program D, translated from MIX to C and acquiring
17558f0484fSRodney W. Grimes * a few minor changes.
17658f0484fSRodney W. Grimes *
17758f0484fSRodney W. Grimes * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
17858f0484fSRodney W. Grimes */
17958f0484fSRodney W. Grimes d = 0;
18058f0484fSRodney W. Grimes for (t = v[1]; t < B / 2; t <<= 1)
18158f0484fSRodney W. Grimes d++;
18258f0484fSRodney W. Grimes if (d > 0) {
18358f0484fSRodney W. Grimes shl(&u[0], m + n, d); /* u <<= d */
18458f0484fSRodney W. Grimes shl(&v[1], n - 1, d); /* v <<= d */
18558f0484fSRodney W. Grimes }
18658f0484fSRodney W. Grimes /*
18758f0484fSRodney W. Grimes * D2: j = 0.
18858f0484fSRodney W. Grimes */
18958f0484fSRodney W. Grimes j = 0;
19058f0484fSRodney W. Grimes v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
19158f0484fSRodney W. Grimes v2 = v[2]; /* for D3 */
19258f0484fSRodney W. Grimes do {
1938fb3f3f6SDavid E. O'Brien digit uj0, uj1, uj2;
19458f0484fSRodney W. Grimes
19558f0484fSRodney W. Grimes /*
19658f0484fSRodney W. Grimes * D3: Calculate qhat (\^q, in TeX notation).
19758f0484fSRodney W. Grimes * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
19858f0484fSRodney W. Grimes * let rhat = (u[j]*B + u[j+1]) mod v[1].
19958f0484fSRodney W. Grimes * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
20058f0484fSRodney W. Grimes * decrement qhat and increase rhat correspondingly.
20158f0484fSRodney W. Grimes * Note that if rhat >= B, v[2]*qhat < rhat*B.
20258f0484fSRodney W. Grimes */
20358f0484fSRodney W. Grimes uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
20458f0484fSRodney W. Grimes uj1 = u[j + 1]; /* for D3 only */
20558f0484fSRodney W. Grimes uj2 = u[j + 2]; /* for D3 only */
20658f0484fSRodney W. Grimes if (uj0 == v1) {
20758f0484fSRodney W. Grimes qhat = B;
20858f0484fSRodney W. Grimes rhat = uj1;
20958f0484fSRodney W. Grimes goto qhat_too_big;
21058f0484fSRodney W. Grimes } else {
21158f0484fSRodney W. Grimes u_long n = COMBINE(uj0, uj1);
21258f0484fSRodney W. Grimes qhat = n / v1;
21358f0484fSRodney W. Grimes rhat = n % v1;
21458f0484fSRodney W. Grimes }
21558f0484fSRodney W. Grimes while (v2 * qhat > COMBINE(rhat, uj2)) {
21658f0484fSRodney W. Grimes qhat_too_big:
21758f0484fSRodney W. Grimes qhat--;
21858f0484fSRodney W. Grimes if ((rhat += v1) >= B)
21958f0484fSRodney W. Grimes break;
22058f0484fSRodney W. Grimes }
22158f0484fSRodney W. Grimes /*
22258f0484fSRodney W. Grimes * D4: Multiply and subtract.
22358f0484fSRodney W. Grimes * The variable `t' holds any borrows across the loop.
22458f0484fSRodney W. Grimes * We split this up so that we do not require v[0] = 0,
22558f0484fSRodney W. Grimes * and to eliminate a final special case.
22658f0484fSRodney W. Grimes */
22758f0484fSRodney W. Grimes for (t = 0, i = n; i > 0; i--) {
22858f0484fSRodney W. Grimes t = u[i + j] - v[i] * qhat - t;
22958f0484fSRodney W. Grimes u[i + j] = LHALF(t);
23058f0484fSRodney W. Grimes t = (B - HHALF(t)) & (B - 1);
23158f0484fSRodney W. Grimes }
23258f0484fSRodney W. Grimes t = u[j] - t;
23358f0484fSRodney W. Grimes u[j] = LHALF(t);
23458f0484fSRodney W. Grimes /*
23558f0484fSRodney W. Grimes * D5: test remainder.
23658f0484fSRodney W. Grimes * There is a borrow if and only if HHALF(t) is nonzero;
23758f0484fSRodney W. Grimes * in that (rare) case, qhat was too large (by exactly 1).
23858f0484fSRodney W. Grimes * Fix it by adding v[1..n] to u[j..j+n].
23958f0484fSRodney W. Grimes */
24058f0484fSRodney W. Grimes if (HHALF(t)) {
24158f0484fSRodney W. Grimes qhat--;
24258f0484fSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
24358f0484fSRodney W. Grimes t += u[i + j] + v[i];
24458f0484fSRodney W. Grimes u[i + j] = LHALF(t);
24558f0484fSRodney W. Grimes t = HHALF(t);
24658f0484fSRodney W. Grimes }
24758f0484fSRodney W. Grimes u[j] = LHALF(u[j] + t);
24858f0484fSRodney W. Grimes }
24958f0484fSRodney W. Grimes q[j] = qhat;
25058f0484fSRodney W. Grimes } while (++j <= m); /* D7: loop on j. */
25158f0484fSRodney W. Grimes
25258f0484fSRodney W. Grimes /*
25358f0484fSRodney W. Grimes * If caller wants the remainder, we have to calculate it as
25458f0484fSRodney W. Grimes * u[m..m+n] >> d (this is at most n digits and thus fits in
25558f0484fSRodney W. Grimes * u[m+1..m+n], but we may need more source digits).
25658f0484fSRodney W. Grimes */
25758f0484fSRodney W. Grimes if (arq) {
25858f0484fSRodney W. Grimes if (d) {
25958f0484fSRodney W. Grimes for (i = m + n; i > m; --i)
26058f0484fSRodney W. Grimes u[i] = (u[i] >> d) |
26158f0484fSRodney W. Grimes LHALF(u[i - 1] << (HALF_BITS - d));
26258f0484fSRodney W. Grimes u[i] = 0;
26358f0484fSRodney W. Grimes }
26458f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
26558f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
26658f0484fSRodney W. Grimes *arq = tmp.q;
26758f0484fSRodney W. Grimes }
26858f0484fSRodney W. Grimes
26958f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
27058f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
27158f0484fSRodney W. Grimes return (tmp.q);
27258f0484fSRodney W. Grimes }
273