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 #if defined(LIBC_SCCS) && !defined(lint) 3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 6/4/93"; 3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 398fb3f3f6SDavid E. O'Brien #include <sys/cdefs.h> 408fb3f3f6SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4158f0484fSRodney W. Grimes 4258f0484fSRodney W. Grimes /* 4358f0484fSRodney W. Grimes * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), 4458f0484fSRodney W. Grimes * section 4.3.1, pp. 257--259. 4558f0484fSRodney W. Grimes */ 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include "quad.h" 4858f0484fSRodney W. Grimes 49feb1d550SDimitry Andric #define B (1L << HALF_BITS) /* digit base */ 5058f0484fSRodney W. Grimes 5158f0484fSRodney W. Grimes /* Combine two `digits' to make a single two-digit number. */ 5258f0484fSRodney W. Grimes #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b)) 5358f0484fSRodney W. Grimes 5458f0484fSRodney W. Grimes /* select a type for digits in base B: use unsigned short if they fit */ 5558f0484fSRodney W. Grimes #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff 5658f0484fSRodney W. Grimes typedef unsigned short digit; 5758f0484fSRodney W. Grimes #else 5858f0484fSRodney W. Grimes typedef u_long digit; 5958f0484fSRodney W. Grimes #endif 6058f0484fSRodney W. Grimes 6158f0484fSRodney W. Grimes /* 6258f0484fSRodney W. Grimes * Shift p[0]..p[len] left `sh' bits, ignoring any bits that 6358f0484fSRodney W. Grimes * `fall out' the left (there never will be any such anyway). 6458f0484fSRodney W. Grimes * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. 6558f0484fSRodney W. Grimes */ 6658f0484fSRodney W. Grimes static void 678fb3f3f6SDavid E. O'Brien shl(digit *p, int len, int sh) 6858f0484fSRodney W. Grimes { 698fb3f3f6SDavid E. O'Brien int i; 7058f0484fSRodney W. Grimes 7158f0484fSRodney W. Grimes for (i = 0; i < len; i++) 7258f0484fSRodney W. Grimes p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh)); 7358f0484fSRodney W. Grimes p[i] = LHALF(p[i] << sh); 7458f0484fSRodney W. Grimes } 7558f0484fSRodney W. Grimes 7658f0484fSRodney W. Grimes /* 7758f0484fSRodney W. Grimes * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. 7858f0484fSRodney W. Grimes * 7958f0484fSRodney W. Grimes * We do this in base 2-sup-HALF_BITS, so that all intermediate products 8058f0484fSRodney W. Grimes * fit within u_long. As a consequence, the maximum length dividend and 8158f0484fSRodney W. Grimes * divisor are 4 `digits' in this base (they are shorter if they have 8258f0484fSRodney W. Grimes * leading zeros). 8358f0484fSRodney W. Grimes */ 8458f0484fSRodney W. Grimes u_quad_t 856492be46SEd Maste __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq) 8658f0484fSRodney W. Grimes { 8758f0484fSRodney W. Grimes union uu tmp; 8858f0484fSRodney W. Grimes digit *u, *v, *q; 898fb3f3f6SDavid E. O'Brien digit v1, v2; 9058f0484fSRodney W. Grimes u_long qhat, rhat, t; 9158f0484fSRodney W. Grimes int m, n, d, j, i; 9258f0484fSRodney W. Grimes digit uspace[5], vspace[5], qspace[5]; 9358f0484fSRodney W. Grimes 9458f0484fSRodney W. Grimes /* 9558f0484fSRodney W. Grimes * Take care of special cases: divide by zero, and u < v. 9658f0484fSRodney W. Grimes */ 97*1024bb26SHans Petter Selasky if (__predict_false(vq == 0)) { 9858f0484fSRodney W. Grimes /* divide by zero. */ 9958f0484fSRodney W. Grimes static volatile const unsigned int zero = 0; 10058f0484fSRodney W. Grimes 10158f0484fSRodney W. Grimes tmp.ul[H] = tmp.ul[L] = 1 / zero; 10258f0484fSRodney W. Grimes if (arq) 10358f0484fSRodney W. Grimes *arq = uq; 10458f0484fSRodney W. Grimes return (tmp.q); 10558f0484fSRodney W. Grimes } 10658f0484fSRodney W. Grimes if (uq < vq) { 10758f0484fSRodney W. Grimes if (arq) 10858f0484fSRodney W. Grimes *arq = uq; 10958f0484fSRodney W. Grimes return (0); 11058f0484fSRodney W. Grimes } 11158f0484fSRodney W. Grimes u = &uspace[0]; 11258f0484fSRodney W. Grimes v = &vspace[0]; 11358f0484fSRodney W. Grimes q = &qspace[0]; 11458f0484fSRodney W. Grimes 11558f0484fSRodney W. Grimes /* 11658f0484fSRodney W. Grimes * Break dividend and divisor into digits in base B, then 11758f0484fSRodney W. Grimes * count leading zeros to determine m and n. When done, we 11858f0484fSRodney W. Grimes * will have: 11958f0484fSRodney W. Grimes * u = (u[1]u[2]...u[m+n]) sub B 12058f0484fSRodney W. Grimes * v = (v[1]v[2]...v[n]) sub B 12158f0484fSRodney W. Grimes * v[1] != 0 12258f0484fSRodney W. Grimes * 1 < n <= 4 (if n = 1, we use a different division algorithm) 12358f0484fSRodney W. Grimes * m >= 0 (otherwise u < v, which we already checked) 12458f0484fSRodney W. Grimes * m + n = 4 12558f0484fSRodney W. Grimes * and thus 12658f0484fSRodney W. Grimes * m = 4 - n <= 2 12758f0484fSRodney W. Grimes */ 12858f0484fSRodney W. Grimes tmp.uq = uq; 12958f0484fSRodney W. Grimes u[0] = 0; 13058f0484fSRodney W. Grimes u[1] = HHALF(tmp.ul[H]); 13158f0484fSRodney W. Grimes u[2] = LHALF(tmp.ul[H]); 13258f0484fSRodney W. Grimes u[3] = HHALF(tmp.ul[L]); 13358f0484fSRodney W. Grimes u[4] = LHALF(tmp.ul[L]); 13458f0484fSRodney W. Grimes tmp.uq = vq; 13558f0484fSRodney W. Grimes v[1] = HHALF(tmp.ul[H]); 13658f0484fSRodney W. Grimes v[2] = LHALF(tmp.ul[H]); 13758f0484fSRodney W. Grimes v[3] = HHALF(tmp.ul[L]); 13858f0484fSRodney W. Grimes v[4] = LHALF(tmp.ul[L]); 13958f0484fSRodney W. Grimes for (n = 4; v[1] == 0; v++) { 14058f0484fSRodney W. Grimes if (--n == 1) { 14158f0484fSRodney W. Grimes u_long rbj; /* r*B+u[j] (not root boy jim) */ 14258f0484fSRodney W. Grimes digit q1, q2, q3, q4; 14358f0484fSRodney W. Grimes 14458f0484fSRodney W. Grimes /* 14558f0484fSRodney W. Grimes * Change of plan, per exercise 16. 14658f0484fSRodney W. Grimes * r = 0; 14758f0484fSRodney W. Grimes * for j = 1..4: 14858f0484fSRodney W. Grimes * q[j] = floor((r*B + u[j]) / v), 14958f0484fSRodney W. Grimes * r = (r*B + u[j]) % v; 15058f0484fSRodney W. Grimes * We unroll this completely here. 15158f0484fSRodney W. Grimes */ 15258f0484fSRodney W. Grimes t = v[2]; /* nonzero, by definition */ 15358f0484fSRodney W. Grimes q1 = u[1] / t; 15458f0484fSRodney W. Grimes rbj = COMBINE(u[1] % t, u[2]); 15558f0484fSRodney W. Grimes q2 = rbj / t; 15658f0484fSRodney W. Grimes rbj = COMBINE(rbj % t, u[3]); 15758f0484fSRodney W. Grimes q3 = rbj / t; 15858f0484fSRodney W. Grimes rbj = COMBINE(rbj % t, u[4]); 15958f0484fSRodney W. Grimes q4 = rbj / t; 16058f0484fSRodney W. Grimes if (arq) 16158f0484fSRodney W. Grimes *arq = rbj % t; 16258f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(q1, q2); 16358f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(q3, q4); 16458f0484fSRodney W. Grimes return (tmp.q); 16558f0484fSRodney W. Grimes } 16658f0484fSRodney W. Grimes } 16758f0484fSRodney W. Grimes 16858f0484fSRodney W. Grimes /* 16958f0484fSRodney W. Grimes * By adjusting q once we determine m, we can guarantee that 17058f0484fSRodney W. Grimes * there is a complete four-digit quotient at &qspace[1] when 17158f0484fSRodney W. Grimes * we finally stop. 17258f0484fSRodney W. Grimes */ 17358f0484fSRodney W. Grimes for (m = 4 - n; u[1] == 0; u++) 17458f0484fSRodney W. Grimes m--; 17558f0484fSRodney W. Grimes for (i = 4 - m; --i >= 0;) 17658f0484fSRodney W. Grimes q[i] = 0; 17758f0484fSRodney W. Grimes q += 4 - m; 17858f0484fSRodney W. Grimes 17958f0484fSRodney W. Grimes /* 18058f0484fSRodney W. Grimes * Here we run Program D, translated from MIX to C and acquiring 18158f0484fSRodney W. Grimes * a few minor changes. 18258f0484fSRodney W. Grimes * 18358f0484fSRodney W. Grimes * D1: choose multiplier 1 << d to ensure v[1] >= B/2. 18458f0484fSRodney W. Grimes */ 18558f0484fSRodney W. Grimes d = 0; 18658f0484fSRodney W. Grimes for (t = v[1]; t < B / 2; t <<= 1) 18758f0484fSRodney W. Grimes d++; 18858f0484fSRodney W. Grimes if (d > 0) { 18958f0484fSRodney W. Grimes shl(&u[0], m + n, d); /* u <<= d */ 19058f0484fSRodney W. Grimes shl(&v[1], n - 1, d); /* v <<= d */ 19158f0484fSRodney W. Grimes } 19258f0484fSRodney W. Grimes /* 19358f0484fSRodney W. Grimes * D2: j = 0. 19458f0484fSRodney W. Grimes */ 19558f0484fSRodney W. Grimes j = 0; 19658f0484fSRodney W. Grimes v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ 19758f0484fSRodney W. Grimes v2 = v[2]; /* for D3 */ 19858f0484fSRodney W. Grimes do { 1998fb3f3f6SDavid E. O'Brien digit uj0, uj1, uj2; 20058f0484fSRodney W. Grimes 20158f0484fSRodney W. Grimes /* 20258f0484fSRodney W. Grimes * D3: Calculate qhat (\^q, in TeX notation). 20358f0484fSRodney W. Grimes * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and 20458f0484fSRodney W. Grimes * let rhat = (u[j]*B + u[j+1]) mod v[1]. 20558f0484fSRodney W. Grimes * While rhat < B and v[2]*qhat > rhat*B+u[j+2], 20658f0484fSRodney W. Grimes * decrement qhat and increase rhat correspondingly. 20758f0484fSRodney W. Grimes * Note that if rhat >= B, v[2]*qhat < rhat*B. 20858f0484fSRodney W. Grimes */ 20958f0484fSRodney W. Grimes uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ 21058f0484fSRodney W. Grimes uj1 = u[j + 1]; /* for D3 only */ 21158f0484fSRodney W. Grimes uj2 = u[j + 2]; /* for D3 only */ 21258f0484fSRodney W. Grimes if (uj0 == v1) { 21358f0484fSRodney W. Grimes qhat = B; 21458f0484fSRodney W. Grimes rhat = uj1; 21558f0484fSRodney W. Grimes goto qhat_too_big; 21658f0484fSRodney W. Grimes } else { 21758f0484fSRodney W. Grimes u_long n = COMBINE(uj0, uj1); 21858f0484fSRodney W. Grimes qhat = n / v1; 21958f0484fSRodney W. Grimes rhat = n % v1; 22058f0484fSRodney W. Grimes } 22158f0484fSRodney W. Grimes while (v2 * qhat > COMBINE(rhat, uj2)) { 22258f0484fSRodney W. Grimes qhat_too_big: 22358f0484fSRodney W. Grimes qhat--; 22458f0484fSRodney W. Grimes if ((rhat += v1) >= B) 22558f0484fSRodney W. Grimes break; 22658f0484fSRodney W. Grimes } 22758f0484fSRodney W. Grimes /* 22858f0484fSRodney W. Grimes * D4: Multiply and subtract. 22958f0484fSRodney W. Grimes * The variable `t' holds any borrows across the loop. 23058f0484fSRodney W. Grimes * We split this up so that we do not require v[0] = 0, 23158f0484fSRodney W. Grimes * and to eliminate a final special case. 23258f0484fSRodney W. Grimes */ 23358f0484fSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { 23458f0484fSRodney W. Grimes t = u[i + j] - v[i] * qhat - t; 23558f0484fSRodney W. Grimes u[i + j] = LHALF(t); 23658f0484fSRodney W. Grimes t = (B - HHALF(t)) & (B - 1); 23758f0484fSRodney W. Grimes } 23858f0484fSRodney W. Grimes t = u[j] - t; 23958f0484fSRodney W. Grimes u[j] = LHALF(t); 24058f0484fSRodney W. Grimes /* 24158f0484fSRodney W. Grimes * D5: test remainder. 24258f0484fSRodney W. Grimes * There is a borrow if and only if HHALF(t) is nonzero; 24358f0484fSRodney W. Grimes * in that (rare) case, qhat was too large (by exactly 1). 24458f0484fSRodney W. Grimes * Fix it by adding v[1..n] to u[j..j+n]. 24558f0484fSRodney W. Grimes */ 24658f0484fSRodney W. Grimes if (HHALF(t)) { 24758f0484fSRodney W. Grimes qhat--; 24858f0484fSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ 24958f0484fSRodney W. Grimes t += u[i + j] + v[i]; 25058f0484fSRodney W. Grimes u[i + j] = LHALF(t); 25158f0484fSRodney W. Grimes t = HHALF(t); 25258f0484fSRodney W. Grimes } 25358f0484fSRodney W. Grimes u[j] = LHALF(u[j] + t); 25458f0484fSRodney W. Grimes } 25558f0484fSRodney W. Grimes q[j] = qhat; 25658f0484fSRodney W. Grimes } while (++j <= m); /* D7: loop on j. */ 25758f0484fSRodney W. Grimes 25858f0484fSRodney W. Grimes /* 25958f0484fSRodney W. Grimes * If caller wants the remainder, we have to calculate it as 26058f0484fSRodney W. Grimes * u[m..m+n] >> d (this is at most n digits and thus fits in 26158f0484fSRodney W. Grimes * u[m+1..m+n], but we may need more source digits). 26258f0484fSRodney W. Grimes */ 26358f0484fSRodney W. Grimes if (arq) { 26458f0484fSRodney W. Grimes if (d) { 26558f0484fSRodney W. Grimes for (i = m + n; i > m; --i) 26658f0484fSRodney W. Grimes u[i] = (u[i] >> d) | 26758f0484fSRodney W. Grimes LHALF(u[i - 1] << (HALF_BITS - d)); 26858f0484fSRodney W. Grimes u[i] = 0; 26958f0484fSRodney W. Grimes } 27058f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(uspace[1], uspace[2]); 27158f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(uspace[3], uspace[4]); 27258f0484fSRodney W. Grimes *arq = tmp.q; 27358f0484fSRodney W. Grimes } 27458f0484fSRodney W. Grimes 27558f0484fSRodney W. Grimes tmp.ul[H] = COMBINE(qspace[1], qspace[2]); 27658f0484fSRodney W. Grimes tmp.ul[L] = COMBINE(qspace[3], qspace[4]); 27758f0484fSRodney W. Grimes return (tmp.q); 27858f0484fSRodney W. Grimes } 279