1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * %sccs.include.redist.c% 10 */ 11 12 #if defined(LIBC_SCCS) && !defined(lint) 13 static char sccsid[] = "@(#)qdivrem.c 5.6 (Berkeley) 06/02/92"; 14 #endif /* LIBC_SCCS and not lint */ 15 16 /* 17 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed), 18 * section 4.3.1, pp. 257--259. 19 */ 20 21 #include "quad.h" 22 23 #define B (1 << HALF_BITS) /* digit base */ 24 25 /* Combine two `digits' to make a single two-digit number. */ 26 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b)) 27 28 /* select a type for digits in base B: use unsigned short if they fit */ 29 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff 30 typedef unsigned short digit; 31 #else 32 typedef u_long digit; 33 #endif 34 35 /* 36 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that 37 * `fall out' the left (there never will be any such anyway). 38 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS. 39 */ 40 static void 41 shl(register digit *p, register int len, register int sh) 42 { 43 register int i; 44 45 for (i = 0; i < len; i++) 46 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh)); 47 p[i] = LHALF(p[i] << sh); 48 } 49 50 /* 51 * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v. 52 * 53 * We do this in base 2-sup-HALF_BITS, so that all intermediate products 54 * fit within u_long. As a consequence, the maximum length dividend and 55 * divisor are 4 `digits' in this base (they are shorter if they have 56 * leading zeros). 57 */ 58 u_quad 59 __qdivrem(u_quad uq, u_quad vq, u_quad *arq) 60 { 61 union uu tmp; 62 digit *u, *v, *q; 63 register digit v1, v2; 64 u_long qhat, rhat, t; 65 int m, n, d, j, i; 66 digit uspace[5], vspace[5], qspace[5]; 67 68 /* 69 * Take care of special cases: divide by zero, and u < v. 70 */ 71 if (vq == 0) { 72 /* divide by zero. */ 73 static volatile const unsigned int zero = 0; 74 75 tmp.ul[H] = tmp.ul[L] = 1 / zero; 76 if (arq) 77 *arq = uq; 78 return (tmp.q); 79 } 80 if (uq < vq) { 81 if (arq) 82 *arq = uq; 83 return (0); 84 } 85 u = &uspace[0]; 86 v = &vspace[0]; 87 q = &qspace[0]; 88 89 /* 90 * Break dividend and divisor into digits in base B, then 91 * count leading zeros to determine m and n. When done, we 92 * will have: 93 * u = (u[1]u[2]...u[m+n]) sub B 94 * v = (v[1]v[2]...v[n]) sub B 95 * v[1] != 0 96 * 1 < n <= 4 (if n = 1, we use a different division algorithm) 97 * m >= 0 (otherwise u < v, which we already checked) 98 * m + n = 4 99 * and thus 100 * m = 4 - n <= 2 101 */ 102 tmp.uq = uq; 103 u[0] = 0; 104 u[1] = HHALF(tmp.ul[H]); 105 u[2] = LHALF(tmp.ul[H]); 106 u[3] = HHALF(tmp.ul[L]); 107 u[4] = LHALF(tmp.ul[L]); 108 tmp.uq = vq; 109 v[1] = HHALF(tmp.ul[H]); 110 v[2] = LHALF(tmp.ul[H]); 111 v[3] = HHALF(tmp.ul[L]); 112 v[4] = LHALF(tmp.ul[L]); 113 for (n = 4; v[1] == 0; v++) { 114 if (--n == 1) { 115 u_long rbj; /* r*B+u[j] (not root boy jim) */ 116 digit q1, q2, q3, q4; 117 118 /* 119 * Change of plan, per exercise 16. 120 * r = 0; 121 * for j = 1..4: 122 * q[j] = floor((r*B + u[j]) / v), 123 * r = (r*B + u[j]) % v; 124 * We unroll this completely here. 125 */ 126 t = v[2]; /* nonzero, by definition */ 127 q1 = u[1] / t; 128 rbj = COMBINE(u[1] % t, u[2]); 129 q2 = rbj / t; 130 rbj = COMBINE(rbj % t, u[3]); 131 q3 = rbj / t; 132 rbj = COMBINE(rbj % t, u[4]); 133 q4 = rbj / t; 134 if (arq) 135 *arq = rbj % t; 136 tmp.ul[H] = COMBINE(q1, q2); 137 tmp.ul[L] = COMBINE(q3, q4); 138 return (tmp.q); 139 } 140 } 141 142 /* 143 * By adjusting q once we determine m, we can guarantee that 144 * there is a complete four-digit quotient at &qspace[1] when 145 * we finally stop. 146 */ 147 for (m = 4 - n; u[1] == 0; u++) 148 m--; 149 for (i = 4 - m; --i >= 0;) 150 q[i] = 0; 151 q += 4 - m; 152 153 /* 154 * Here we run Program D, translated from MIX to C and acquiring 155 * a few minor changes. 156 * 157 * D1: choose multiplier 1 << d to ensure v[1] >= B/2. 158 */ 159 d = 0; 160 for (t = v[1]; t < B / 2; t <<= 1) 161 d++; 162 if (d > 0) { 163 shl(&u[0], m + n, d); /* u <<= d */ 164 shl(&v[1], n - 1, d); /* v <<= d */ 165 } 166 /* 167 * D2: j = 0. 168 */ 169 j = 0; 170 v1 = v[1]; /* for D3 -- note that v[1..n] are constant */ 171 v2 = v[2]; /* for D3 */ 172 do { 173 register digit uj0, uj1, uj2; 174 175 /* 176 * D3: Calculate qhat (\^q, in TeX notation). 177 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and 178 * let rhat = (u[j]*B + u[j+1]) mod v[1]. 179 * While rhat < B and v[2]*qhat > rhat*B+u[j+2], 180 * decrement qhat and increase rhat correspondingly. 181 * Note that if rhat >= B, v[2]*qhat < rhat*B. 182 */ 183 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */ 184 uj1 = u[j + 1]; /* for D3 only */ 185 uj2 = u[j + 2]; /* for D3 only */ 186 if (uj0 == v1) { 187 qhat = B; 188 rhat = uj1; 189 goto qhat_too_big; 190 } else { 191 u_long n = COMBINE(uj0, uj1); 192 qhat = n / v1; 193 rhat = n % v1; 194 } 195 while (v2 * qhat > COMBINE(rhat, uj2)) { 196 qhat_too_big: 197 qhat--; 198 if ((rhat += v1) >= B) 199 break; 200 } 201 /* 202 * D4: Multiply and subtract. 203 * The variable `t' holds any borrows across the loop. 204 * We split this up so that we do not require v[0] = 0, 205 * and to eliminate a final special case. 206 */ 207 for (t = 0, i = n; i > 0; i--) { 208 t = u[i + j] - v[i] * qhat - t; 209 u[i + j] = LHALF(t); 210 t = (B - HHALF(t)) & (B - 1); 211 } 212 t = u[j] - t; 213 u[j] = LHALF(t); 214 /* 215 * D5: test remainder. 216 * There is a borrow if and only if HHALF(t) is nonzero; 217 * in that (rare) case, qhat was too large (by exactly 1). 218 * Fix it by adding v[1..n] to u[j..j+n]. 219 */ 220 if (HHALF(t)) { 221 qhat--; 222 for (t = 0, i = n; i > 0; i--) { /* D6: add back. */ 223 t += u[i + j] + v[i]; 224 u[i + j] = LHALF(t); 225 t = HHALF(t); 226 } 227 u[j] = LHALF(u[j] + t); 228 } 229 q[j] = qhat; 230 } while (++j <= m); /* D7: loop on j. */ 231 232 /* 233 * If caller wants the remainder, we have to calculate it as 234 * u[m..m+n] >> d (this is at most n digits and thus fits in 235 * u[m+1..m+n], but we may need more source digits). 236 */ 237 if (arq) { 238 if (d) { 239 for (i = m + n; i > m; --i) 240 u[i] = (u[i] >> d) | 241 LHALF(u[i - 1] << (HALF_BITS - d)); 242 u[i] = 0; 243 } 244 tmp.ul[H] = COMBINE(uspace[1], uspace[2]); 245 tmp.ul[L] = COMBINE(uspace[3], uspace[4]); 246 *arq = tmp.q; 247 } 248 249 tmp.ul[H] = COMBINE(qspace[1], qspace[2]); 250 tmp.ul[L] = COMBINE(qspace[3], qspace[4]); 251 return (tmp.q); 252 } 253