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