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