qdivrem.c revision 8870
1249259Sdim/*- 2249259Sdim * Copyright (c) 1992, 1993 3249259Sdim * The Regents of the University of California. All rights reserved. 4249259Sdim * 5249259Sdim * This software was developed by the Computer Systems Engineering group 6249259Sdim * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7249259Sdim * contributed to Berkeley. 8249259Sdim * 9249259Sdim * Redistribution and use in source and binary forms, with or without 10249259Sdim * modification, are permitted provided that the following conditions 11249259Sdim * are met: 12249259Sdim * 1. Redistributions of source code must retain the above copyright 13249259Sdim * notice, this list of conditions and the following disclaimer. 14249259Sdim * 2. Redistributions in binary form must reproduce the above copyright 15249259Sdim * notice, this list of conditions and the following disclaimer in the 16249259Sdim * documentation and/or other materials provided with the distribution. 17249259Sdim * 3. All advertising materials mentioning features or use of this software 18249259Sdim * must display the following acknowledgement: 19249259Sdim * This product includes software developed by the University of 20249259Sdim * California, Berkeley and its contributors. 21249259Sdim * 4. Neither the name of the University nor the names of its contributors 22249259Sdim * may be used to endorse or promote products derived from this software 23249259Sdim * without specific prior written permission. 24249259Sdim * 25276479Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28249259Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30280031Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35249259Sdim * SUCH DAMAGE. 36249259Sdim */ 37280031Sdim 38249259Sdim#if defined(LIBC_SCCS) && !defined(lint) 39249259Sdimstatic char sccsid[] = "@(#)qdivrem.c 8.1 (Berkeley) 6/4/93"; 40249259Sdim#endif /* LIBC_SCCS and not lint */ 41249259Sdim 42249259Sdim/* 43249259Sdim * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), 44249259Sdim * section 4.3.1, pp. 257--259. 45249259Sdim */ 46249259Sdim 47249259Sdim#include "quad.h" 48249259Sdim 49249259Sdim#define B (1 << HALF_BITS) /* digit base */ 50249259Sdim 51249259Sdim/* Combine two `digits' to make a single two-digit number. */ 52249259Sdim#define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b)) 53249259Sdim 54249259Sdim/* select a type for digits in base B: use unsigned short if they fit */ 55249259Sdim#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff 56276479Sdimtypedef unsigned short digit; 57249259Sdim#else 58249259Sdimtypedef u_long digit; 59249259Sdim#endif 60249259Sdim 61249259Sdim/* 62249259Sdim * Shift p[0]..p[len] left `sh' bits, ignoring any bits that 63249259Sdim * `fall out' the left (there never will be any such anyway). 64249259Sdim * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. 65249259Sdim */ 66249259Sdimstatic void 67249259Sdimshl(register digit *p, register int len, register int sh) 68249259Sdim{ 69249259Sdim register int i; 70249259Sdim 71249259Sdim for (i = 0; i < len; i++) 72249259Sdim p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh)); 73249259Sdim p[i] = LHALF(p[i] << sh); 74249259Sdim} 75249259Sdim 76249259Sdim/* 77249259Sdim * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. 78249259Sdim * 79249259Sdim * We do this in base 2-sup-HALF_BITS, so that all intermediate products 80261991Sdim * fit within u_long. As a consequence, the maximum length dividend and 81249259Sdim * divisor are 4 `digits' in this base (they are shorter if they have 82249259Sdim * leading zeros). 83249259Sdim */ 84249259Sdimu_quad_t 85249259Sdim__qdivrem(uq, vq, arq) 86249259Sdim u_quad_t uq, vq, *arq; 87249259Sdim{ 88249259Sdim union uu tmp; 89249259Sdim digit *u, *v, *q; 90249259Sdim register digit v1, v2; 91249259Sdim u_long qhat, rhat, t; 92261991Sdim int m, n, d, j, i; 93261991Sdim digit uspace[5], vspace[5], qspace[5]; 94261991Sdim 95249259Sdim /* 96249259Sdim * Take care of special cases: divide by zero, and u < v. 97249259Sdim */ 98249259Sdim if (vq == 0) { 99276479Sdim /* divide by zero. */ 100249259Sdim static volatile const unsigned int zero = 0; 101249259Sdim 102249259Sdim tmp.ul[H] = tmp.ul[L] = 1 / zero; 103249259Sdim if (arq) 104249259Sdim *arq = uq; 105249259Sdim return (tmp.q); 106249259Sdim } 107249259Sdim if (uq < vq) { 108249259Sdim if (arq) 109249259Sdim *arq = uq; 110249259Sdim return (0); 111249259Sdim } 112249259Sdim u = &uspace[0]; 113249259Sdim v = &vspace[0]; 114249259Sdim q = &qspace[0]; 115249259Sdim 116249259Sdim /* 117249259Sdim * Break dividend and divisor into digits in base B, then 118249259Sdim * count leading zeros to determine m and n. When done, we 119249259Sdim * will have: 120249259Sdim * u = (u[1]u[2]...u[m+n]) sub B 121249259Sdim * v = (v[1]v[2]...v[n]) sub B 122249259Sdim * v[1] != 0 123249259Sdim * 1 < n <= 4 (if n = 1, we use a different division algorithm) 124249259Sdim * m >= 0 (otherwise u < v, which we already checked) 125249259Sdim * m + n = 4 126249259Sdim * and thus 127249259Sdim * m = 4 - n <= 2 128249259Sdim */ 129249259Sdim tmp.uq = uq; 130249259Sdim u[0] = 0; 131249259Sdim u[1] = HHALF(tmp.ul[H]); 132249259Sdim u[2] = LHALF(tmp.ul[H]); 133249259Sdim u[3] = HHALF(tmp.ul[L]); 134249259Sdim u[4] = LHALF(tmp.ul[L]); 135288943Sdim tmp.uq = vq; 136288943Sdim v[1] = HHALF(tmp.ul[H]); 137249259Sdim v[2] = LHALF(tmp.ul[H]); 138249259Sdim v[3] = HHALF(tmp.ul[L]); 139249259Sdim v[4] = LHALF(tmp.ul[L]); 140249259Sdim for (n = 4; v[1] == 0; v++) { 141249259Sdim if (--n == 1) { 142249259Sdim u_long rbj; /* r*B+u[j] (not root boy jim) */ 143249259Sdim digit q1, q2, q3, q4; 144249259Sdim 145276479Sdim /* 146249259Sdim * Change of plan, per exercise 16. 147249259Sdim * r = 0; 148249259Sdim * for j = 1..4: 149249259Sdim * q[j] = floor((r*B + u[j]) / v), 150249259Sdim * r = (r*B + u[j]) % v; 151249259Sdim * We unroll this completely here. 152249259Sdim */ 153249259Sdim t = v[2]; /* nonzero, by definition */ 154249259Sdim q1 = u[1] / t; 155249259Sdim rbj = COMBINE(u[1] % t, u[2]); 156249259Sdim q2 = rbj / t; 157249259Sdim rbj = COMBINE(rbj % t, u[3]); 158249259Sdim q3 = rbj / t; 159249259Sdim rbj = COMBINE(rbj % t, u[4]); 160249259Sdim q4 = rbj / t; 161249259Sdim if (arq) 162249259Sdim *arq = rbj % t; 163249259Sdim tmp.ul[H] = COMBINE(q1, q2); 164249259Sdim tmp.ul[L] = COMBINE(q3, q4); 165249259Sdim return (tmp.q); 166249259Sdim } 167249259Sdim } 168249259Sdim 169249259Sdim /* 170249259Sdim * By adjusting q once we determine m, we can guarantee that 171249259Sdim * there is a complete four-digit quotient at &qspace[1] when 172249259Sdim * we finally stop. 173288943Sdim */ 174288943Sdim for (m = 4 - n; u[1] == 0; u++) 175249259Sdim m--; 176249259Sdim for (i = 4 - m; --i >= 0;) 177249259Sdim q[i] = 0; 178249259Sdim q += 4 - m; 179249259Sdim 180276479Sdim /* 181249259Sdim * Here we run Program D, translated from MIX to C and acquiring 182249259Sdim * a few minor changes. 183249259Sdim * 184288943Sdim * D1: choose multiplier 1 << d to ensure v[1] >= B/2. 185288943Sdim */ 186288943Sdim d = 0; 187288943Sdim for (t = v[1]; t < B / 2; t <<= 1) 188288943Sdim d++; 189288943Sdim if (d > 0) { 190288943Sdim shl(&u[0], m + n, d); /* u <<= d */ 191288943Sdim shl(&v[1], n - 1, d); /* v <<= d */ 192288943Sdim } 193288943Sdim /* 194249259Sdim * D2: j = 0. 195249259Sdim */ 196288943Sdim j = 0; 197249259Sdim v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ 198276479Sdim v2 = v[2]; /* for D3 */ 199249259Sdim do { 200249259Sdim register digit uj0, uj1, uj2; 201249259Sdim 202249259Sdim /* 203249259Sdim * D3: Calculate qhat (\^q, in TeX notation). 204249259Sdim * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and 205249259Sdim * let rhat = (u[j]*B + u[j+1]) mod v[1]. 206249259Sdim * While rhat < B and v[2]*qhat > rhat*B+u[j+2], 207249259Sdim * decrement qhat and increase rhat correspondingly. 208249259Sdim * Note that if rhat >= B, v[2]*qhat < rhat*B. 209249259Sdim */ 210249259Sdim uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ 211249259Sdim uj1 = u[j + 1]; /* for D3 only */ 212249259Sdim uj2 = u[j + 2]; /* for D3 only */ 213249259Sdim if (uj0 == v1) { 214249259Sdim qhat = B; 215249259Sdim rhat = uj1; 216249259Sdim goto qhat_too_big; 217249259Sdim } else { 218249259Sdim u_long n = COMBINE(uj0, uj1); 219249259Sdim qhat = n / v1; 220249259Sdim rhat = n % v1; 221249259Sdim } 222249259Sdim while (v2 * qhat > COMBINE(rhat, uj2)) { 223249259Sdim qhat_too_big: 224249259Sdim qhat--; 225249259Sdim if ((rhat += v1) >= B) 226249259Sdim break; 227249259Sdim } 228249259Sdim /* 229249259Sdim * D4: Multiply and subtract. 230249259Sdim * The variable `t' holds any borrows across the loop. 231249259Sdim * We split this up so that we do not require v[0] = 0, 232249259Sdim * and to eliminate a final special case. 233276479Sdim */ 234276479Sdim for (t = 0, i = n; i > 0; i--) { 235249259Sdim t = u[i + j] - v[i] * qhat - t; 236276479Sdim u[i + j] = LHALF(t); 237249259Sdim t = (B - HHALF(t)) & (B - 1); 238249259Sdim } 239276479Sdim t = u[j] - t; 240276479Sdim u[j] = LHALF(t); 241249259Sdim /* 242249259Sdim * D5: test remainder. 243249259Sdim * There is a borrow if and only if HHALF(t) is nonzero; 244249259Sdim * in that (rare) case, qhat was too large (by exactly 1). 245249259Sdim * Fix it by adding v[1..n] to u[j..j+n]. 246249259Sdim */ 247249259Sdim if (HHALF(t)) { 248276479Sdim qhat--; 249276479Sdim for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ 250249259Sdim t += u[i + j] + v[i]; 251249259Sdim u[i + j] = LHALF(t); 252249259Sdim t = HHALF(t); 253249259Sdim } 254276479Sdim u[j] = LHALF(u[j] + t); 255276479Sdim } 256249259Sdim q[j] = qhat; 257249259Sdim } while (++j <= m); /* D7: loop on j. */ 258249259Sdim 259249259Sdim /* 260249259Sdim * If caller wants the remainder, we have to calculate it as 261249259Sdim * u[m..m+n] >> d (this is at most n digits and thus fits in 262276479Sdim * u[m+1..m+n], but we may need more source digits). 263276479Sdim */ 264249259Sdim if (arq) { 265249259Sdim if (d) { 266249259Sdim for (i = m + n; i > m; --i) 267249259Sdim u[i] = (u[i] >> d) | 268276479Sdim LHALF(u[i - 1] << (HALF_BITS - d)); 269276479Sdim u[i] = 0; 270249259Sdim } 271249259Sdim tmp.ul[H] = COMBINE(uspace[1], uspace[2]); 272249259Sdim tmp.ul[L] = COMBINE(uspace[3], uspace[4]); 273276479Sdim *arq = tmp.q; 274249259Sdim } 275249259Sdim 276249259Sdim tmp.ul[H] = COMBINE(qspace[1], qspace[2]); 277249259Sdim tmp.ul[L] = COMBINE(qspace[3], qspace[4]); 278249259Sdim return (tmp.q); 279249259Sdim} 280249259Sdim