199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1992, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by the Computer Systems Engineering group
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * contributed to Berkeley.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * From: Id: qdivrem.c,v 1.7 1997/11/07 09:20:40 phk Exp
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * section 4.3.1, pp. 257--259.
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Combine two `digits' to make a single two-digit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b))
199767f8919635c4928607450d9e0abb932109ceToomas Soome_Static_assert(sizeof(int) / 2 == sizeof(short),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Bitwise functions in libstand are broken on this architecture\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* select a type for digits in base B: use unsigned short if they fit */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * `fall out' the left (there never will be any such anyway).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We do this in base 2-sup-HALF_BITS, so that all intermediate products
199767f8919635c4928607450d9e0abb932109ceToomas Soome * fit within u_int. As a consequence, the maximum length dividend and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * divisor are 4 `digits' in this base (they are shorter if they have
199767f8919635c4928607450d9e0abb932109ceToomas Soome * leading zeros).
199767f8919635c4928607450d9e0abb932109ceToomas Soome int m, n, d, j, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Take care of special cases: divide by zero, and u < v.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* divide by zero. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Break dividend and divisor into digits in base B, then
199767f8919635c4928607450d9e0abb932109ceToomas Soome * count leading zeros to determine m and n. When done, we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * will have:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * u = (u[1]u[2]...u[m+n]) sub B
199767f8919635c4928607450d9e0abb932109ceToomas Soome * v = (v[1]v[2]...v[n]) sub B
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1 < n <= 4 (if n = 1, we use a different division algorithm)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * m >= 0 (otherwise u < v, which we already checked)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * m = 4 - n <= 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (--n == 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Change of plan, per exercise 16.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for j = 1..4:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * q[j] = floor((r*B + u[j]) / v),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * r = (r*B + u[j]) % v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We unroll this completely here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * By adjusting q once we determine m, we can guarantee that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * there is a complete four-digit quotient at &qspace[1] when
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we finally stop.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 4 - m; --i >= 0;)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Here we run Program D, translated from MIX to C and acquiring
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a few minor changes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D2: j = 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D3: Calculate qhat (\^q, in TeX notation).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * let rhat = (u[j]*B + u[j+1]) mod v[1].
199767f8919635c4928607450d9e0abb932109ceToomas Soome * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
199767f8919635c4928607450d9e0abb932109ceToomas Soome * decrement qhat and increase rhat correspondingly.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that if rhat >= B, v[2]*qhat < rhat*B.
199767f8919635c4928607450d9e0abb932109ceToomas Soome uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D4: Multiply and subtract.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The variable `t' holds any borrows across the loop.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We split this up so that we do not require v[0] = 0,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and to eliminate a final special case.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (t = 0, i = n; i > 0; i--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = u[i + j] - v[i] * qhat - t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[i + j] = LHALF(t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = u[j] - t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D5: test remainder.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There is a borrow if and only if HHALF(t) is nonzero;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in that (rare) case, qhat was too large (by exactly 1).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Fix it by adding v[1..n] to u[j..j+n].
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome t += u[i + j] + v[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[i + j] = LHALF(t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[j] = LHALF(u[j] + t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (++j <= m); /* D7: loop on j. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If caller wants the remainder, we have to calculate it as
199767f8919635c4928607450d9e0abb932109ceToomas Soome * u[m..m+n] >> d (this is at most n digits and thus fits in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * u[m+1..m+n], but we may need more source digits).
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = m + n; i > m; --i)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[i] = (u[i] >> d) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Divide two unsigned quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return remainder after dividing two unsigned quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)__qdivrem(a, b, &r);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Divide two signed quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ??? if -1/2 should produce -1 on this machine, this code is wrong
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return remainder after dividing two signed quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If -1/2 should produce -1 on this machine, this code is wrong.