00001 /*- 00002 * Copyright (c) 1992, 1993 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * This software was developed by the Computer Systems Engineering group 00006 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 00007 * contributed to Berkeley. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 2. Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the distribution. 00017 * 3. Neither the name of the University nor the names of its contributors 00018 * may be used to endorse or promote products derived from this software 00019 * without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 * 00033 * From: 00034 * @(#)qdivrem.c 8.1 (Berkeley) 6/4/93 00035 * NetBSD: qdivrem.c,v 1.1 2005/12/20 19:28:51 christos Exp 00036 */ 00037 00038 /* 00039 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), 00040 * section 4.3.1, pp. 257--259. 00041 */ 00042 00043 #include "longlong.h" 00044 00045 #define B ((int)1 << HALF_BITS) /* digit base */ 00046 00047 /* Combine two `digits' to make a single two-digit number. */ 00048 #define COMBINE(a, b) (((unsigned int)(a) << HALF_BITS) | (b)) 00049 00050 /* select a type for digits in base B: use unsigned short if they fit */ 00051 #if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff 00052 typedef unsigned short digit; 00053 #else 00054 typedef unsigned int digit; 00055 #endif 00056 00057 static void shl(digit *p, int len, int sh); 00058 00059 /* 00060 * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. 00061 * 00062 * We do this in base 2-sup-HALF_BITS, so that all intermediate 00063 * products fit within unsigned int. As a consequence, the maximum 00064 * length dividend and divisor are 4 `digits' in this base (they are 00065 * shorter if they have leading zeros). 00066 */ 00067 unsigned long long 00068 __qdivrem(unsigned long long ull, unsigned long long vll, 00069 unsigned long long *arq) 00070 { 00071 union uu tmp; 00072 digit *u, *v, *q; 00073 digit v1, v2; 00074 unsigned int qhat, rhat, t; 00075 int m, n, d, j, i; 00076 digit uspace[5], vspace[5], qspace[5]; 00077 00078 /* 00079 * Take care of special cases: divide by zero, and u < v. 00080 */ 00081 if (vll == 0) { 00082 /* divide by zero. */ 00083 static volatile const unsigned int zero = 0; 00084 00085 tmp.ui[H] = tmp.ui[L] = 1 / zero; 00086 if (arq) 00087 *arq = ull; 00088 return (tmp.ll); 00089 } 00090 if (ull < vll) { 00091 if (arq) 00092 *arq = ull; 00093 return (0); 00094 } 00095 u = &uspace[0]; 00096 v = &vspace[0]; 00097 q = &qspace[0]; 00098 00099 /* 00100 * Break dividend and divisor into digits in base B, then 00101 * count leading zeros to determine m and n. When done, we 00102 * will have: 00103 * u = (u[1]u[2]...u[m+n]) sub B 00104 * v = (v[1]v[2]...v[n]) sub B 00105 * v[1] != 0 00106 * 1 < n <= 4 (if n = 1, we use a different division algorithm) 00107 * m >= 0 (otherwise u < v, which we already checked) 00108 * m + n = 4 00109 * and thus 00110 * m = 4 - n <= 2 00111 */ 00112 tmp.ull = ull; 00113 u[0] = 0; 00114 u[1] = (digit)HHALF(tmp.ui[H]); 00115 u[2] = (digit)LHALF(tmp.ui[H]); 00116 u[3] = (digit)HHALF(tmp.ui[L]); 00117 u[4] = (digit)LHALF(tmp.ui[L]); 00118 tmp.ull = vll; 00119 v[1] = (digit)HHALF(tmp.ui[H]); 00120 v[2] = (digit)LHALF(tmp.ui[H]); 00121 v[3] = (digit)HHALF(tmp.ui[L]); 00122 v[4] = (digit)LHALF(tmp.ui[L]); 00123 for (n = 4; v[1] == 0; v++) { 00124 if (--n == 1) { 00125 unsigned int rbj; /* r*B+u[j] (not root boy jim) */ 00126 digit q1, q2, q3, q4; 00127 00128 /* 00129 * Change of plan, per exercise 16. 00130 * r = 0; 00131 * for j = 1..4: 00132 * q[j] = floor((r*B + u[j]) / v), 00133 * r = (r*B + u[j]) % v; 00134 * We unroll this completely here. 00135 */ 00136 t = v[2]; /* nonzero, by definition */ 00137 q1 = (digit)(u[1] / t); 00138 rbj = COMBINE(u[1] % t, u[2]); 00139 q2 = (digit)(rbj / t); 00140 rbj = COMBINE(rbj % t, u[3]); 00141 q3 = (digit)(rbj / t); 00142 rbj = COMBINE(rbj % t, u[4]); 00143 q4 = (digit)(rbj / t); 00144 if (arq) 00145 *arq = rbj % t; 00146 tmp.ui[H] = COMBINE(q1, q2); 00147 tmp.ui[L] = COMBINE(q3, q4); 00148 return (tmp.ll); 00149 } 00150 } 00151 00152 /* 00153 * By adjusting q once we determine m, we can guarantee that 00154 * there is a complete four-digit quotient at &qspace[1] when 00155 * we finally stop. 00156 */ 00157 for (m = 4 - n; u[1] == 0; u++) 00158 m--; 00159 for (i = 4 - m; --i >= 0;) 00160 q[i] = 0; 00161 q += 4 - m; 00162 00163 /* 00164 * Here we run Program D, translated from MIX to C and acquiring 00165 * a few minor changes. 00166 * 00167 * D1: choose multiplier 1 << d to ensure v[1] >= B/2. 00168 */ 00169 d = 0; 00170 for (t = v[1]; t < B / 2; t <<= 1) 00171 d++; 00172 if (d > 0) { 00173 shl(&u[0], m + n, d); /* u <<= d */ 00174 shl(&v[1], n - 1, d); /* v <<= d */ 00175 } 00176 /* 00177 * D2: j = 0. 00178 */ 00179 j = 0; 00180 v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ 00181 v2 = v[2]; /* for D3 */ 00182 do { 00183 digit uj0, uj1, uj2; 00184 00185 /* 00186 * D3: Calculate qhat (\^q, in TeX notation). 00187 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and 00188 * let rhat = (u[j]*B + u[j+1]) mod v[1]. 00189 * While rhat < B and v[2]*qhat > rhat*B+u[j+2], 00190 * decrement qhat and increase rhat correspondingly. 00191 * Note that if rhat >= B, v[2]*qhat < rhat*B. 00192 */ 00193 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ 00194 uj1 = u[j + 1]; /* for D3 only */ 00195 uj2 = u[j + 2]; /* for D3 only */ 00196 if (uj0 == v1) { 00197 qhat = B; 00198 rhat = uj1; 00199 goto qhat_too_big; 00200 } else { 00201 unsigned int nn = COMBINE(uj0, uj1); 00202 qhat = nn / v1; 00203 rhat = nn % v1; 00204 } 00205 while (v2 * qhat > COMBINE(rhat, uj2)) { 00206 qhat_too_big: 00207 qhat--; 00208 if ((rhat += v1) >= B) 00209 break; 00210 } 00211 /* 00212 * D4: Multiply and subtract. 00213 * The variable `t' holds any borrows across the loop. 00214 * We split this up so that we do not require v[0] = 0, 00215 * and to eliminate a final special case. 00216 */ 00217 for (t = 0, i = n; i > 0; i--) { 00218 t = u[i + j] - v[i] * qhat - t; 00219 u[i + j] = (digit)LHALF(t); 00220 t = (B - HHALF(t)) & (B - 1); 00221 } 00222 t = u[j] - t; 00223 u[j] = (digit)LHALF(t); 00224 /* 00225 * D5: test remainder. 00226 * There is a borrow if and only if HHALF(t) is nonzero; 00227 * in that (rare) case, qhat was too large (by exactly 1). 00228 * Fix it by adding v[1..n] to u[j..j+n]. 00229 */ 00230 if (HHALF(t)) { 00231 qhat--; 00232 for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ 00233 t += u[i + j] + v[i]; 00234 u[i + j] = (digit)LHALF(t); 00235 t = HHALF(t); 00236 } 00237 u[j] = (digit)LHALF(u[j] + t); 00238 } 00239 q[j] = (digit)qhat; 00240 } while (++j <= m); /* D7: loop on j. */ 00241 00242 /* 00243 * If caller wants the remainder, we have to calculate it as 00244 * u[m..m+n] >> d (this is at most n digits and thus fits in 00245 * u[m+1..m+n], but we may need more source digits). 00246 */ 00247 if (arq) { 00248 if (d) { 00249 for (i = m + n; i > m; --i) 00250 u[i] = (digit)(((unsigned int)u[i] >> d) | 00251 LHALF((unsigned int)u[i - 1] << 00252 (HALF_BITS - d))); 00253 u[i] = 0; 00254 } 00255 tmp.ui[H] = COMBINE(uspace[1], uspace[2]); 00256 tmp.ui[L] = COMBINE(uspace[3], uspace[4]); 00257 *arq = tmp.ll; 00258 } 00259 00260 tmp.ui[H] = COMBINE(qspace[1], qspace[2]); 00261 tmp.ui[L] = COMBINE(qspace[3], qspace[4]); 00262 return (tmp.ll); 00263 } 00264 00265 /* 00266 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that 00267 * `fall out' the left (there never will be any such anyway). 00268 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. 00269 */ 00270 static void 00271 shl(digit *p, int len, int sh) 00272 { 00273 int i; 00274 00275 for (i = 0; i < len; i++) 00276 p[i] = (digit)(LHALF((unsigned int)p[i] << sh) | 00277 ((unsigned int)p[i + 1] >> (HALF_BITS - sh))); 00278 p[i] = (digit)(LHALF((unsigned int)p[i] << sh)); 00279 }