/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992, 19934* The Regents of the University of California. All rights reserved.5*6* This software was developed by the Computer Systems Engineering group7* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and8* contributed to Berkeley.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/cdefs.h>36/*37* Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),38* section 4.3.1, pp. 257--259.39*/4041#include <libkern/quad.h>4243#define B (1 << HALF_BITS) /* digit base */4445/* Combine two `digits' to make a single two-digit number. */46#define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))4748/* select a type for digits in base B: use unsigned short if they fit */49#if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff50typedef unsigned short digit;51#else52typedef u_long digit;53#endif5455/*56* Shift p[0]..p[len] left `sh' bits, ignoring any bits that57* `fall out' the left (there never will be any such anyway).58* We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.59*/60static void61__shl(digit *p, int len, int sh)62{63int i;6465for (i = 0; i < len; i++)66p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));67p[i] = LHALF(p[i] << sh);68}6970/*71* __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.72*73* We do this in base 2-sup-HALF_BITS, so that all intermediate products74* fit within u_long. As a consequence, the maximum length dividend and75* divisor are 4 `digits' in this base (they are shorter if they have76* leading zeros).77*/78u_quad_t79__qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)80{81union uu tmp;82digit *u, *v, *q;83digit v1, v2;84u_long qhat, rhat, t;85int m, n, d, j, i;86digit uspace[5], vspace[5], qspace[5];8788/*89* Take care of special cases: divide by zero, and u < v.90*/91if (__predict_false(vq == 0)) {92/* divide by zero. */93static volatile const unsigned int zero = 0;9495tmp.ul[H] = tmp.ul[L] = 1 / zero;96if (arq)97*arq = uq;98return (tmp.q);99}100if (uq < vq) {101if (arq)102*arq = uq;103return (0);104}105u = &uspace[0];106v = &vspace[0];107q = &qspace[0];108109/*110* Break dividend and divisor into digits in base B, then111* count leading zeros to determine m and n. When done, we112* will have:113* u = (u[1]u[2]...u[m+n]) sub B114* v = (v[1]v[2]...v[n]) sub B115* v[1] != 0116* 1 < n <= 4 (if n = 1, we use a different division algorithm)117* m >= 0 (otherwise u < v, which we already checked)118* m + n = 4119* and thus120* m = 4 - n <= 2121*/122tmp.uq = uq;123u[0] = 0;124u[1] = HHALF(tmp.ul[H]);125u[2] = LHALF(tmp.ul[H]);126u[3] = HHALF(tmp.ul[L]);127u[4] = LHALF(tmp.ul[L]);128tmp.uq = vq;129v[1] = HHALF(tmp.ul[H]);130v[2] = LHALF(tmp.ul[H]);131v[3] = HHALF(tmp.ul[L]);132v[4] = LHALF(tmp.ul[L]);133for (n = 4; v[1] == 0; v++) {134if (--n == 1) {135u_long rbj; /* r*B+u[j] (not root boy jim) */136digit q1, q2, q3, q4;137138/*139* Change of plan, per exercise 16.140* r = 0;141* for j = 1..4:142* q[j] = floor((r*B + u[j]) / v),143* r = (r*B + u[j]) % v;144* We unroll this completely here.145*/146t = v[2]; /* nonzero, by definition */147q1 = u[1] / t;148rbj = COMBINE(u[1] % t, u[2]);149q2 = rbj / t;150rbj = COMBINE(rbj % t, u[3]);151q3 = rbj / t;152rbj = COMBINE(rbj % t, u[4]);153q4 = rbj / t;154if (arq)155*arq = rbj % t;156tmp.ul[H] = COMBINE(q1, q2);157tmp.ul[L] = COMBINE(q3, q4);158return (tmp.q);159}160}161162/*163* By adjusting q once we determine m, we can guarantee that164* there is a complete four-digit quotient at &qspace[1] when165* we finally stop.166*/167for (m = 4 - n; u[1] == 0; u++)168m--;169for (i = 4 - m; --i >= 0;)170q[i] = 0;171q += 4 - m;172173/*174* Here we run Program D, translated from MIX to C and acquiring175* a few minor changes.176*177* D1: choose multiplier 1 << d to ensure v[1] >= B/2.178*/179d = 0;180for (t = v[1]; t < B / 2; t <<= 1)181d++;182if (d > 0) {183__shl(&u[0], m + n, d); /* u <<= d */184__shl(&v[1], n - 1, d); /* v <<= d */185}186/*187* D2: j = 0.188*/189j = 0;190v1 = v[1]; /* for D3 -- note that v[1..n] are constant */191v2 = v[2]; /* for D3 */192do {193digit uj0, uj1, uj2;194195/*196* D3: Calculate qhat (\^q, in TeX notation).197* Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and198* let rhat = (u[j]*B + u[j+1]) mod v[1].199* While rhat < B and v[2]*qhat > rhat*B+u[j+2],200* decrement qhat and increase rhat correspondingly.201* Note that if rhat >= B, v[2]*qhat < rhat*B.202*/203uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */204uj1 = u[j + 1]; /* for D3 only */205uj2 = u[j + 2]; /* for D3 only */206if (uj0 == v1) {207qhat = B;208rhat = uj1;209goto qhat_too_big;210} else {211u_long nn = COMBINE(uj0, uj1);212qhat = nn / v1;213rhat = nn % v1;214}215while (v2 * qhat > COMBINE(rhat, uj2)) {216qhat_too_big:217qhat--;218if ((rhat += v1) >= B)219break;220}221/*222* D4: Multiply and subtract.223* The variable `t' holds any borrows across the loop.224* We split this up so that we do not require v[0] = 0,225* and to eliminate a final special case.226*/227for (t = 0, i = n; i > 0; i--) {228t = u[i + j] - v[i] * qhat - t;229u[i + j] = LHALF(t);230t = (B - HHALF(t)) & (B - 1);231}232t = u[j] - t;233u[j] = LHALF(t);234/*235* D5: test remainder.236* There is a borrow if and only if HHALF(t) is nonzero;237* in that (rare) case, qhat was too large (by exactly 1).238* Fix it by adding v[1..n] to u[j..j+n].239*/240if (HHALF(t)) {241qhat--;242for (t = 0, i = n; i > 0; i--) { /* D6: add back. */243t += u[i + j] + v[i];244u[i + j] = LHALF(t);245t = HHALF(t);246}247u[j] = LHALF(u[j] + t);248}249q[j] = qhat;250} while (++j <= m); /* D7: loop on j. */251252/*253* If caller wants the remainder, we have to calculate it as254* u[m..m+n] >> d (this is at most n digits and thus fits in255* u[m+1..m+n], but we may need more source digits).256*/257if (arq) {258if (d) {259for (i = m + n; i > m; --i)260u[i] = (u[i] >> d) |261LHALF(u[i - 1] << (HALF_BITS - d));262u[i] = 0;263}264tmp.ul[H] = COMBINE(uspace[1], uspace[2]);265tmp.ul[L] = COMBINE(uspace[3], uspace[4]);266*arq = tmp.q;267}268269tmp.ul[H] = COMBINE(qspace[1], qspace[2]);270tmp.ul[L] = COMBINE(qspace[3], qspace[4]);271return (tmp.q);272}273274275