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