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