199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1992, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 * are met:
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 *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * From: Id: qdivrem.c,v 1.7 1997/11/07 09:20:40 phk Exp
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * section 4.3.1, pp. 257--259.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "quad.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B (1 << HALF_BITS) /* digit base */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome_Static_assert(sizeof(int) / 2 == sizeof(short),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Bitwise functions in libstand are broken on this architecture\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* select a type for digits in base B: use unsigned short if they fit */
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef unsigned short digit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeshl(digit *p, int len, int sh)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
199767f8919635c4928607450d9e0abb932109ceToomas Soome p[i] = LHALF(p[i] << sh);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome__qdivrem(uq, vq, arq)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t uq, vq, *arq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome union uu tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome digit *u, *v, *q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome digit v1, v2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int qhat, rhat, t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int m, n, d, j, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome digit uspace[5], vspace[5], qspace[5];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Take care of special cases: divide by zero, and u < v.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vq == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* divide by zero. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome static volatile const unsigned int zero = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[H] = tmp.ul[L] = 1 / zero;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (arq)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *arq = uq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tmp.q);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (uq < vq) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (arq)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *arq = uq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome u = &uspace[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome v = &vspace[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome q = &qspace[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 * v[1] != 0
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 + n = 4
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and thus
199767f8919635c4928607450d9e0abb932109ceToomas Soome * m = 4 - n <= 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.uq = uq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[0] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[1] = HHALF(tmp.ul[H]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[2] = LHALF(tmp.ul[H]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[3] = HHALF(tmp.ul[L]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[4] = LHALF(tmp.ul[L]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.uq = vq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v[1] = HHALF(tmp.ul[H]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v[2] = LHALF(tmp.ul[H]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v[3] = HHALF(tmp.ul[L]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v[4] = LHALF(tmp.ul[L]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (n = 4; v[1] == 0; v++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (--n == 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int rbj; /* r*B+u[j] (not root boy jim) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome digit q1, q2, q3, q4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Change of plan, per exercise 16.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * r = 0;
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = v[2]; /* nonzero, by definition */
199767f8919635c4928607450d9e0abb932109ceToomas Soome q1 = u[1] / t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rbj = COMBINE(u[1] % t, u[2]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome q2 = rbj / t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rbj = COMBINE(rbj % t, u[3]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome q3 = rbj / t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rbj = COMBINE(rbj % t, u[4]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome q4 = rbj / t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (arq)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *arq = rbj % t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[H] = COMBINE(q1, q2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[L] = COMBINE(q3, q4);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tmp.q);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (m = 4 - n; u[1] == 0; u++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome m--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 4 - m; --i >= 0;)
199767f8919635c4928607450d9e0abb932109ceToomas Soome q[i] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome q += 4 - m;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Here we run Program D, translated from MIX to C and acquiring
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a few minor changes.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (t = v[1]; t < B / 2; t <<= 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome d++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome shl(&u[0], m + n, d); /* u <<= d */
199767f8919635c4928607450d9e0abb932109ceToomas Soome shl(&v[1], n - 1, d); /* v <<= d */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * D2: j = 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome j = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
199767f8919635c4928607450d9e0abb932109ceToomas Soome v2 = v[2]; /* for D3 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome digit uj0, uj1, uj2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uj1 = u[j + 1]; /* for D3 only */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uj2 = u[j + 2]; /* for D3 only */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (uj0 == v1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome qhat = B;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rhat = uj1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto qhat_too_big;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int nn = COMBINE(uj0, uj1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome qhat = nn / v1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rhat = nn % v1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (v2 * qhat > COMBINE(rhat, uj2)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome qhat_too_big:
199767f8919635c4928607450d9e0abb932109ceToomas Soome qhat--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rhat += v1) >= B)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
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 = (B - HHALF(t)) & (B - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = u[j] - t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[j] = LHALF(t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (HHALF(t)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome qhat--;
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 t = HHALF(t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[j] = LHALF(u[j] + t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome q[j] = qhat;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (++j <= m); /* D7: loop on j. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (arq) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = m + n; i > m; --i)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[i] = (u[i] >> d) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome LHALF(u[i - 1] << (HALF_BITS - d));
199767f8919635c4928607450d9e0abb932109ceToomas Soome u[i] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *arq = tmp.q;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tmp.q);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Divide two unsigned quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome__udivdi3(a, b)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t a, b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__qdivrem(a, b, (u_quad_t *)0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return remainder after dividing two unsigned quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeu_quad_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome__umoddi3(a, b)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t a, b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t r;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)__qdivrem(a, b, &r);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (r);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Divide two signed quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ??? if -1/2 should produce -1 on this machine, this code is wrong
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomequad_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome__divdi3(a, b)
199767f8919635c4928607450d9e0abb932109ceToomas Soome quad_t a, b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t ua, ub, uq;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int neg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (a < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ua = -(u_quad_t)a, neg = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ua = a, neg = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub = -(u_quad_t)b, neg ^= 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub = b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uq = __qdivrem(ua, ub, (u_quad_t *)0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (neg ? -uq : uq);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Return remainder after dividing two signed quads.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If -1/2 should produce -1 on this machine, this code is wrong.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomequad_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome__moddi3(a, b)
199767f8919635c4928607450d9e0abb932109ceToomas Soome quad_t a, b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_quad_t ua, ub, ur;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int neg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (a < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ua = -(u_quad_t)a, neg = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ua = a, neg = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub = -(u_quad_t)b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub = b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)__qdivrem(ua, ub, &ur);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (neg ? -ur : ur);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}