1df8bae1dSRodney W. Grimes /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1992, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This software was developed by the Computer Systems Engineering group 8df8bae1dSRodney W. Grimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9df8bae1dSRodney W. Grimes * contributed to Berkeley. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 13df8bae1dSRodney W. Grimes * are met: 14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 18df8bae1dSRodney 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 20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 21df8bae1dSRodney W. Grimes * without specific prior written permission. 22df8bae1dSRodney W. Grimes * 23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df8bae1dSRodney W. Grimes * SUCH DAMAGE. 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36ab0de15bSDavid E. O'Brien #include <sys/cdefs.h> 37ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38ab0de15bSDavid E. O'Brien 39df8bae1dSRodney W. Grimes /* 40df8bae1dSRodney W. Grimes * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), 41df8bae1dSRodney W. Grimes * section 4.3.1, pp. 257--259. 42df8bae1dSRodney W. Grimes */ 43df8bae1dSRodney W. Grimes 44f313170dSBruce Evans #include <libkern/quad.h> 45df8bae1dSRodney W. Grimes 46df8bae1dSRodney W. Grimes #define B (1 << HALF_BITS) /* digit base */ 47df8bae1dSRodney W. Grimes 48df8bae1dSRodney W. Grimes /* Combine two `digits' to make a single two-digit number. */ 49df8bae1dSRodney W. Grimes #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b)) 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes /* select a type for digits in base B: use unsigned short if they fit */ 52df8bae1dSRodney W. Grimes #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff 53df8bae1dSRodney W. Grimes typedef unsigned short digit; 54df8bae1dSRodney W. Grimes #else 55df8bae1dSRodney W. Grimes typedef u_long digit; 56df8bae1dSRodney W. Grimes #endif 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes /* 59df8bae1dSRodney W. Grimes * Shift p[0]..p[len] left `sh' bits, ignoring any bits that 60df8bae1dSRodney W. Grimes * `fall out' the left (there never will be any such anyway). 61df8bae1dSRodney W. Grimes * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. 62df8bae1dSRodney W. Grimes */ 63df8bae1dSRodney W. Grimes static void 64484820d4SConrad Meyer __shl(digit *p, int len, int sh) 65df8bae1dSRodney W. Grimes { 66484820d4SConrad Meyer int i; 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes for (i = 0; i < len; i++) 69df8bae1dSRodney W. Grimes p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh)); 70df8bae1dSRodney W. Grimes p[i] = LHALF(p[i] << sh); 71df8bae1dSRodney W. Grimes } 72df8bae1dSRodney W. Grimes 73df8bae1dSRodney W. Grimes /* 74df8bae1dSRodney W. Grimes * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. 75df8bae1dSRodney W. Grimes * 76df8bae1dSRodney W. Grimes * We do this in base 2-sup-HALF_BITS, so that all intermediate products 77df8bae1dSRodney W. Grimes * fit within u_long. As a consequence, the maximum length dividend and 78df8bae1dSRodney W. Grimes * divisor are 4 `digits' in this base (they are shorter if they have 79df8bae1dSRodney W. Grimes * leading zeros). 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes u_quad_t 824a8dea8cSEd Maste __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq) 83df8bae1dSRodney W. Grimes { 84df8bae1dSRodney W. Grimes union uu tmp; 85df8bae1dSRodney W. Grimes digit *u, *v, *q; 86484820d4SConrad Meyer digit v1, v2; 87df8bae1dSRodney W. Grimes u_long qhat, rhat, t; 88df8bae1dSRodney W. Grimes int m, n, d, j, i; 89df8bae1dSRodney W. Grimes digit uspace[5], vspace[5], qspace[5]; 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes /* 92df8bae1dSRodney W. Grimes * Take care of special cases: divide by zero, and u < v. 93df8bae1dSRodney W. Grimes */ 94df8bae1dSRodney W. Grimes if (vq == 0) { 95df8bae1dSRodney W. Grimes /* divide by zero. */ 96df8bae1dSRodney W. Grimes static volatile const unsigned int zero = 0; 97df8bae1dSRodney W. Grimes 98df8bae1dSRodney W. Grimes tmp.ul[H] = tmp.ul[L] = 1 / zero; 99df8bae1dSRodney W. Grimes if (arq) 100df8bae1dSRodney W. Grimes *arq = uq; 101df8bae1dSRodney W. Grimes return (tmp.q); 102df8bae1dSRodney W. Grimes } 103df8bae1dSRodney W. Grimes if (uq < vq) { 104df8bae1dSRodney W. Grimes if (arq) 105df8bae1dSRodney W. Grimes *arq = uq; 106df8bae1dSRodney W. Grimes return (0); 107df8bae1dSRodney W. Grimes } 108df8bae1dSRodney W. Grimes u = &uspace[0]; 109df8bae1dSRodney W. Grimes v = &vspace[0]; 110df8bae1dSRodney W. Grimes q = &qspace[0]; 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes /* 113df8bae1dSRodney W. Grimes * Break dividend and divisor into digits in base B, then 114df8bae1dSRodney W. Grimes * count leading zeros to determine m and n. When done, we 115df8bae1dSRodney W. Grimes * will have: 116df8bae1dSRodney W. Grimes * u = (u[1]u[2]...u[m+n]) sub B 117df8bae1dSRodney W. Grimes * v = (v[1]v[2]...v[n]) sub B 118df8bae1dSRodney W. Grimes * v[1] != 0 119df8bae1dSRodney W. Grimes * 1 < n <= 4 (if n = 1, we use a different division algorithm) 120df8bae1dSRodney W. Grimes * m >= 0 (otherwise u < v, which we already checked) 121df8bae1dSRodney W. Grimes * m + n = 4 122df8bae1dSRodney W. Grimes * and thus 123df8bae1dSRodney W. Grimes * m = 4 - n <= 2 124df8bae1dSRodney W. Grimes */ 125df8bae1dSRodney W. Grimes tmp.uq = uq; 126df8bae1dSRodney W. Grimes u[0] = 0; 127df8bae1dSRodney W. Grimes u[1] = HHALF(tmp.ul[H]); 128df8bae1dSRodney W. Grimes u[2] = LHALF(tmp.ul[H]); 129df8bae1dSRodney W. Grimes u[3] = HHALF(tmp.ul[L]); 130df8bae1dSRodney W. Grimes u[4] = LHALF(tmp.ul[L]); 131df8bae1dSRodney W. Grimes tmp.uq = vq; 132df8bae1dSRodney W. Grimes v[1] = HHALF(tmp.ul[H]); 133df8bae1dSRodney W. Grimes v[2] = LHALF(tmp.ul[H]); 134df8bae1dSRodney W. Grimes v[3] = HHALF(tmp.ul[L]); 135df8bae1dSRodney W. Grimes v[4] = LHALF(tmp.ul[L]); 136df8bae1dSRodney W. Grimes for (n = 4; v[1] == 0; v++) { 137df8bae1dSRodney W. Grimes if (--n == 1) { 138df8bae1dSRodney W. Grimes u_long rbj; /* r*B+u[j] (not root boy jim) */ 139df8bae1dSRodney W. Grimes digit q1, q2, q3, q4; 140df8bae1dSRodney W. Grimes 141df8bae1dSRodney W. Grimes /* 142df8bae1dSRodney W. Grimes * Change of plan, per exercise 16. 143df8bae1dSRodney W. Grimes * r = 0; 144df8bae1dSRodney W. Grimes * for j = 1..4: 145df8bae1dSRodney W. Grimes * q[j] = floor((r*B + u[j]) / v), 146df8bae1dSRodney W. Grimes * r = (r*B + u[j]) % v; 147df8bae1dSRodney W. Grimes * We unroll this completely here. 148df8bae1dSRodney W. Grimes */ 149df8bae1dSRodney W. Grimes t = v[2]; /* nonzero, by definition */ 150df8bae1dSRodney W. Grimes q1 = u[1] / t; 151df8bae1dSRodney W. Grimes rbj = COMBINE(u[1] % t, u[2]); 152df8bae1dSRodney W. Grimes q2 = rbj / t; 153df8bae1dSRodney W. Grimes rbj = COMBINE(rbj % t, u[3]); 154df8bae1dSRodney W. Grimes q3 = rbj / t; 155df8bae1dSRodney W. Grimes rbj = COMBINE(rbj % t, u[4]); 156df8bae1dSRodney W. Grimes q4 = rbj / t; 157df8bae1dSRodney W. Grimes if (arq) 158df8bae1dSRodney W. Grimes *arq = rbj % t; 159df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(q1, q2); 160df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(q3, q4); 161df8bae1dSRodney W. Grimes return (tmp.q); 162df8bae1dSRodney W. Grimes } 163df8bae1dSRodney W. Grimes } 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes /* 166df8bae1dSRodney W. Grimes * By adjusting q once we determine m, we can guarantee that 167df8bae1dSRodney W. Grimes * there is a complete four-digit quotient at &qspace[1] when 168df8bae1dSRodney W. Grimes * we finally stop. 169df8bae1dSRodney W. Grimes */ 170df8bae1dSRodney W. Grimes for (m = 4 - n; u[1] == 0; u++) 171df8bae1dSRodney W. Grimes m--; 172df8bae1dSRodney W. Grimes for (i = 4 - m; --i >= 0;) 173df8bae1dSRodney W. Grimes q[i] = 0; 174df8bae1dSRodney W. Grimes q += 4 - m; 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes /* 177df8bae1dSRodney W. Grimes * Here we run Program D, translated from MIX to C and acquiring 178df8bae1dSRodney W. Grimes * a few minor changes. 179df8bae1dSRodney W. Grimes * 180df8bae1dSRodney W. Grimes * D1: choose multiplier 1 << d to ensure v[1] >= B/2. 181df8bae1dSRodney W. Grimes */ 182df8bae1dSRodney W. Grimes d = 0; 183df8bae1dSRodney W. Grimes for (t = v[1]; t < B / 2; t <<= 1) 184df8bae1dSRodney W. Grimes d++; 185df8bae1dSRodney W. Grimes if (d > 0) { 1866ff5e8a6SAndrew Thompson __shl(&u[0], m + n, d); /* u <<= d */ 1876ff5e8a6SAndrew Thompson __shl(&v[1], n - 1, d); /* v <<= d */ 188df8bae1dSRodney W. Grimes } 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * D2: j = 0. 191df8bae1dSRodney W. Grimes */ 192df8bae1dSRodney W. Grimes j = 0; 193df8bae1dSRodney W. Grimes v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ 194df8bae1dSRodney W. Grimes v2 = v[2]; /* for D3 */ 195df8bae1dSRodney W. Grimes do { 196484820d4SConrad Meyer digit uj0, uj1, uj2; 197df8bae1dSRodney W. Grimes 198df8bae1dSRodney W. Grimes /* 199df8bae1dSRodney W. Grimes * D3: Calculate qhat (\^q, in TeX notation). 200df8bae1dSRodney W. Grimes * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and 201df8bae1dSRodney W. Grimes * let rhat = (u[j]*B + u[j+1]) mod v[1]. 202df8bae1dSRodney W. Grimes * While rhat < B and v[2]*qhat > rhat*B+u[j+2], 203df8bae1dSRodney W. Grimes * decrement qhat and increase rhat correspondingly. 204df8bae1dSRodney W. Grimes * Note that if rhat >= B, v[2]*qhat < rhat*B. 205df8bae1dSRodney W. Grimes */ 206df8bae1dSRodney W. Grimes uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ 207df8bae1dSRodney W. Grimes uj1 = u[j + 1]; /* for D3 only */ 208df8bae1dSRodney W. Grimes uj2 = u[j + 2]; /* for D3 only */ 209df8bae1dSRodney W. Grimes if (uj0 == v1) { 210df8bae1dSRodney W. Grimes qhat = B; 211df8bae1dSRodney W. Grimes rhat = uj1; 212df8bae1dSRodney W. Grimes goto qhat_too_big; 213df8bae1dSRodney W. Grimes } else { 2140abc78a6SPoul-Henning Kamp u_long nn = COMBINE(uj0, uj1); 2150abc78a6SPoul-Henning Kamp qhat = nn / v1; 2160abc78a6SPoul-Henning Kamp rhat = nn % v1; 217df8bae1dSRodney W. Grimes } 218df8bae1dSRodney W. Grimes while (v2 * qhat > COMBINE(rhat, uj2)) { 219df8bae1dSRodney W. Grimes qhat_too_big: 220df8bae1dSRodney W. Grimes qhat--; 221df8bae1dSRodney W. Grimes if ((rhat += v1) >= B) 222df8bae1dSRodney W. Grimes break; 223df8bae1dSRodney W. Grimes } 224df8bae1dSRodney W. Grimes /* 225df8bae1dSRodney W. Grimes * D4: Multiply and subtract. 226df8bae1dSRodney W. Grimes * The variable `t' holds any borrows across the loop. 227df8bae1dSRodney W. Grimes * We split this up so that we do not require v[0] = 0, 228df8bae1dSRodney W. Grimes * and to eliminate a final special case. 229df8bae1dSRodney W. Grimes */ 230df8bae1dSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { 231df8bae1dSRodney W. Grimes t = u[i + j] - v[i] * qhat - t; 232df8bae1dSRodney W. Grimes u[i + j] = LHALF(t); 233df8bae1dSRodney W. Grimes t = (B - HHALF(t)) & (B - 1); 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes t = u[j] - t; 236df8bae1dSRodney W. Grimes u[j] = LHALF(t); 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * D5: test remainder. 239df8bae1dSRodney W. Grimes * There is a borrow if and only if HHALF(t) is nonzero; 240df8bae1dSRodney W. Grimes * in that (rare) case, qhat was too large (by exactly 1). 241df8bae1dSRodney W. Grimes * Fix it by adding v[1..n] to u[j..j+n]. 242df8bae1dSRodney W. Grimes */ 243df8bae1dSRodney W. Grimes if (HHALF(t)) { 244df8bae1dSRodney W. Grimes qhat--; 245df8bae1dSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ 246df8bae1dSRodney W. Grimes t += u[i + j] + v[i]; 247df8bae1dSRodney W. Grimes u[i + j] = LHALF(t); 248df8bae1dSRodney W. Grimes t = HHALF(t); 249df8bae1dSRodney W. Grimes } 250df8bae1dSRodney W. Grimes u[j] = LHALF(u[j] + t); 251df8bae1dSRodney W. Grimes } 252df8bae1dSRodney W. Grimes q[j] = qhat; 253df8bae1dSRodney W. Grimes } while (++j <= m); /* D7: loop on j. */ 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * If caller wants the remainder, we have to calculate it as 257df8bae1dSRodney W. Grimes * u[m..m+n] >> d (this is at most n digits and thus fits in 258df8bae1dSRodney W. Grimes * u[m+1..m+n], but we may need more source digits). 259df8bae1dSRodney W. Grimes */ 260df8bae1dSRodney W. Grimes if (arq) { 261df8bae1dSRodney W. Grimes if (d) { 262df8bae1dSRodney W. Grimes for (i = m + n; i > m; --i) 263df8bae1dSRodney W. Grimes u[i] = (u[i] >> d) | 264df8bae1dSRodney W. Grimes LHALF(u[i - 1] << (HALF_BITS - d)); 265df8bae1dSRodney W. Grimes u[i] = 0; 266df8bae1dSRodney W. Grimes } 267df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(uspace[1], uspace[2]); 268df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(uspace[3], uspace[4]); 269df8bae1dSRodney W. Grimes *arq = tmp.q; 270df8bae1dSRodney W. Grimes } 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(qspace[1], qspace[2]); 273df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(qspace[3], qspace[4]); 274df8bae1dSRodney W. Grimes return (tmp.q); 275df8bae1dSRodney W. Grimes } 276