/*-1* Copyright (c) 1992, 19932* The Regents of the University of California. All rights reserved.3*4* This software was developed by the Computer Systems Engineering group5* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and6* contributed to Berkeley.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*32* From:33* @(#)qdivrem.c 8.1 (Berkeley) 6/4/9334* NetBSD: qdivrem.c,v 1.1 2005/12/20 19:28:51 christos Exp35*/3637/*38* Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),39* section 4.3.1, pp. 257--259.40*/4142#include "longlong.h"4344#define B ((int)1 << HALF_BITS) /* digit base */4546/* Combine two `digits' to make a single two-digit number. */47#define COMBINE(a, b) (((unsigned int)(a) << HALF_BITS) | (b))4849/* select a type for digits in base B: use unsigned short if they fit */50#if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff51typedef unsigned short digit;52#else53typedef unsigned int digit;54#endif5556static void shl(digit *p, int len, int sh);5758/*59* __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.60*61* We do this in base 2-sup-HALF_BITS, so that all intermediate62* products fit within unsigned int. As a consequence, the maximum63* length dividend and divisor are 4 `digits' in this base (they are64* shorter if they have leading zeros).65*/66unsigned long long67__qdivrem(unsigned long long ull, unsigned long long vll,68unsigned long long *arq)69{70union uu tmp;71digit *u, *v, *q;72digit v1, v2;73unsigned int qhat, rhat, t;74int m, n, d, j, i;75digit uspace[5], vspace[5], qspace[5];7677/*78* Take care of special cases: divide by zero, and u < v.79*/80if (vll == 0) {81/* divide by zero. */82static volatile const unsigned int zero = 0;8384tmp.ui[H] = tmp.ui[L] = 1 / zero;85if (arq)86*arq = ull;87return (tmp.ll);88}89if (ull < vll) {90if (arq)91*arq = ull;92return (0);93}94u = &uspace[0];95v = &vspace[0];96q = &qspace[0];9798/*99* Break dividend and divisor into digits in base B, then100* count leading zeros to determine m and n. When done, we101* will have:102* u = (u[1]u[2]...u[m+n]) sub B103* v = (v[1]v[2]...v[n]) sub B104* v[1] != 0105* 1 < n <= 4 (if n = 1, we use a different division algorithm)106* m >= 0 (otherwise u < v, which we already checked)107* m + n = 4108* and thus109* m = 4 - n <= 2110*/111tmp.ull = ull;112u[0] = 0;113u[1] = (digit)HHALF(tmp.ui[H]);114u[2] = (digit)LHALF(tmp.ui[H]);115u[3] = (digit)HHALF(tmp.ui[L]);116u[4] = (digit)LHALF(tmp.ui[L]);117tmp.ull = vll;118v[1] = (digit)HHALF(tmp.ui[H]);119v[2] = (digit)LHALF(tmp.ui[H]);120v[3] = (digit)HHALF(tmp.ui[L]);121v[4] = (digit)LHALF(tmp.ui[L]);122for (n = 4; v[1] == 0; v++) {123if (--n == 1) {124unsigned int rbj; /* r*B+u[j] (not root boy jim) */125digit q1, q2, q3, q4;126127/*128* Change of plan, per exercise 16.129* r = 0;130* for j = 1..4:131* q[j] = floor((r*B + u[j]) / v),132* r = (r*B + u[j]) % v;133* We unroll this completely here.134*/135t = v[2]; /* nonzero, by definition */136q1 = (digit)(u[1] / t);137rbj = COMBINE(u[1] % t, u[2]);138q2 = (digit)(rbj / t);139rbj = COMBINE(rbj % t, u[3]);140q3 = (digit)(rbj / t);141rbj = COMBINE(rbj % t, u[4]);142q4 = (digit)(rbj / t);143if (arq)144*arq = rbj % t;145tmp.ui[H] = COMBINE(q1, q2);146tmp.ui[L] = COMBINE(q3, q4);147return (tmp.ll);148}149}150151/*152* By adjusting q once we determine m, we can guarantee that153* there is a complete four-digit quotient at &qspace[1] when154* we finally stop.155*/156for (m = 4 - n; u[1] == 0; u++)157m--;158for (i = 4 - m; --i >= 0;)159q[i] = 0;160q += 4 - m;161162/*163* Here we run Program D, translated from MIX to C and acquiring164* a few minor changes.165*166* D1: choose multiplier 1 << d to ensure v[1] >= B/2.167*/168d = 0;169for (t = v[1]; t < B / 2; t <<= 1)170d++;171if (d > 0) {172shl(&u[0], m + n, d); /* u <<= d */173shl(&v[1], n - 1, d); /* v <<= d */174}175/*176* D2: j = 0.177*/178j = 0;179v1 = v[1]; /* for D3 -- note that v[1..n] are constant */180v2 = v[2]; /* for D3 */181do {182digit uj0, uj1, uj2;183184/*185* D3: Calculate qhat (\^q, in TeX notation).186* Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and187* let rhat = (u[j]*B + u[j+1]) mod v[1].188* While rhat < B and v[2]*qhat > rhat*B+u[j+2],189* decrement qhat and increase rhat correspondingly.190* Note that if rhat >= B, v[2]*qhat < rhat*B.191*/192uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */193uj1 = u[j + 1]; /* for D3 only */194uj2 = u[j + 2]; /* for D3 only */195if (uj0 == v1) {196qhat = B;197rhat = uj1;198goto qhat_too_big;199} else {200unsigned int nn = COMBINE(uj0, uj1);201qhat = nn / v1;202rhat = nn % v1;203}204while (v2 * qhat > COMBINE(rhat, uj2)) {205qhat_too_big:206qhat--;207if ((rhat += v1) >= B)208break;209}210/*211* D4: Multiply and subtract.212* The variable `t' holds any borrows across the loop.213* We split this up so that we do not require v[0] = 0,214* and to eliminate a final special case.215*/216for (t = 0, i = n; i > 0; i--) {217t = u[i + j] - v[i] * qhat - t;218u[i + j] = (digit)LHALF(t);219t = (B - HHALF(t)) & (B - 1);220}221t = u[j] - t;222u[j] = (digit)LHALF(t);223/*224* D5: test remainder.225* There is a borrow if and only if HHALF(t) is nonzero;226* in that (rare) case, qhat was too large (by exactly 1).227* Fix it by adding v[1..n] to u[j..j+n].228*/229if (HHALF(t)) {230qhat--;231for (t = 0, i = n; i > 0; i--) { /* D6: add back. */232t += u[i + j] + v[i];233u[i + j] = (digit)LHALF(t);234t = HHALF(t);235}236u[j] = (digit)LHALF(u[j] + t);237}238q[j] = (digit)qhat;239} while (++j <= m); /* D7: loop on j. */240241/*242* If caller wants the remainder, we have to calculate it as243* u[m..m+n] >> d (this is at most n digits and thus fits in244* u[m+1..m+n], but we may need more source digits).245*/246if (arq) {247if (d) {248for (i = m + n; i > m; --i)249u[i] = (digit)(((unsigned int)u[i] >> d) |250LHALF((unsigned int)u[i - 1] <<251(HALF_BITS - d)));252u[i] = 0;253}254tmp.ui[H] = COMBINE(uspace[1], uspace[2]);255tmp.ui[L] = COMBINE(uspace[3], uspace[4]);256*arq = tmp.ll;257}258259tmp.ui[H] = COMBINE(qspace[1], qspace[2]);260tmp.ui[L] = COMBINE(qspace[3], qspace[4]);261return (tmp.ll);262}263264/*265* Shift p[0]..p[len] left `sh' bits, ignoring any bits that266* `fall out' the left (there never will be any such anyway).267* We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.268*/269static void270shl(digit *p, int len, int sh)271{272int i;273274for (i = 0; i < len; i++)275p[i] = (digit)(LHALF((unsigned int)p[i] << sh) |276((unsigned int)p[i + 1] >> (HALF_BITS - sh)));277p[i] = (digit)(LHALF((unsigned int)p[i] << sh));278}279280281