1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1992, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * This software was developed by the Computer Systems Engineering group
8df8bae1dSRodney W. Grimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9df8bae1dSRodney W. Grimes * contributed to Berkeley.
10df8bae1dSRodney W. Grimes *
11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes * are met:
14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
21df8bae1dSRodney W. Grimes * without specific prior written permission.
22df8bae1dSRodney W. Grimes *
23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33df8bae1dSRodney W. Grimes * SUCH DAMAGE.
34df8bae1dSRodney W. Grimes */
35df8bae1dSRodney W. Grimes
36ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
37df8bae1dSRodney W. Grimes /*
38df8bae1dSRodney W. Grimes * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
39df8bae1dSRodney W. Grimes * section 4.3.1, pp. 257--259.
40df8bae1dSRodney W. Grimes */
41df8bae1dSRodney W. Grimes
42f313170dSBruce Evans #include <libkern/quad.h>
43df8bae1dSRodney W. Grimes
44df8bae1dSRodney W. Grimes #define B (1 << HALF_BITS) /* digit base */
45df8bae1dSRodney W. Grimes
46df8bae1dSRodney W. Grimes /* Combine two `digits' to make a single two-digit number. */
47df8bae1dSRodney W. Grimes #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
48df8bae1dSRodney W. Grimes
49df8bae1dSRodney W. Grimes /* select a type for digits in base B: use unsigned short if they fit */
50df8bae1dSRodney W. Grimes #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
51df8bae1dSRodney W. Grimes typedef unsigned short digit;
52df8bae1dSRodney W. Grimes #else
53df8bae1dSRodney W. Grimes typedef u_long digit;
54df8bae1dSRodney W. Grimes #endif
55df8bae1dSRodney W. Grimes
56df8bae1dSRodney W. Grimes /*
57df8bae1dSRodney W. Grimes * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
58df8bae1dSRodney W. Grimes * `fall out' the left (there never will be any such anyway).
59df8bae1dSRodney W. Grimes * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
60df8bae1dSRodney W. Grimes */
61df8bae1dSRodney W. Grimes static void
__shl(digit * p,int len,int sh)62484820d4SConrad Meyer __shl(digit *p, int len, int sh)
63df8bae1dSRodney W. Grimes {
64484820d4SConrad Meyer int i;
65df8bae1dSRodney W. Grimes
66df8bae1dSRodney W. Grimes for (i = 0; i < len; i++)
67df8bae1dSRodney W. Grimes p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
68df8bae1dSRodney W. Grimes p[i] = LHALF(p[i] << sh);
69df8bae1dSRodney W. Grimes }
70df8bae1dSRodney W. Grimes
71df8bae1dSRodney W. Grimes /*
72df8bae1dSRodney W. Grimes * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
73df8bae1dSRodney W. Grimes *
74df8bae1dSRodney W. Grimes * We do this in base 2-sup-HALF_BITS, so that all intermediate products
75df8bae1dSRodney W. Grimes * fit within u_long. As a consequence, the maximum length dividend and
76df8bae1dSRodney W. Grimes * divisor are 4 `digits' in this base (they are shorter if they have
77df8bae1dSRodney W. Grimes * leading zeros).
78df8bae1dSRodney W. Grimes */
79df8bae1dSRodney W. Grimes u_quad_t
__qdivrem(u_quad_t uq,u_quad_t vq,u_quad_t * arq)804a8dea8cSEd Maste __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)
81df8bae1dSRodney W. Grimes {
82df8bae1dSRodney W. Grimes union uu tmp;
83df8bae1dSRodney W. Grimes digit *u, *v, *q;
84484820d4SConrad Meyer digit v1, v2;
85df8bae1dSRodney W. Grimes u_long qhat, rhat, t;
86df8bae1dSRodney W. Grimes int m, n, d, j, i;
87df8bae1dSRodney W. Grimes digit uspace[5], vspace[5], qspace[5];
88df8bae1dSRodney W. Grimes
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes * Take care of special cases: divide by zero, and u < v.
91df8bae1dSRodney W. Grimes */
92*1024bb26SHans Petter Selasky if (__predict_false(vq == 0)) {
93df8bae1dSRodney W. Grimes /* divide by zero. */
94df8bae1dSRodney W. Grimes static volatile const unsigned int zero = 0;
95df8bae1dSRodney W. Grimes
96df8bae1dSRodney W. Grimes tmp.ul[H] = tmp.ul[L] = 1 / zero;
97df8bae1dSRodney W. Grimes if (arq)
98df8bae1dSRodney W. Grimes *arq = uq;
99df8bae1dSRodney W. Grimes return (tmp.q);
100df8bae1dSRodney W. Grimes }
101df8bae1dSRodney W. Grimes if (uq < vq) {
102df8bae1dSRodney W. Grimes if (arq)
103df8bae1dSRodney W. Grimes *arq = uq;
104df8bae1dSRodney W. Grimes return (0);
105df8bae1dSRodney W. Grimes }
106df8bae1dSRodney W. Grimes u = &uspace[0];
107df8bae1dSRodney W. Grimes v = &vspace[0];
108df8bae1dSRodney W. Grimes q = &qspace[0];
109df8bae1dSRodney W. Grimes
110df8bae1dSRodney W. Grimes /*
111df8bae1dSRodney W. Grimes * Break dividend and divisor into digits in base B, then
112df8bae1dSRodney W. Grimes * count leading zeros to determine m and n. When done, we
113df8bae1dSRodney W. Grimes * will have:
114df8bae1dSRodney W. Grimes * u = (u[1]u[2]...u[m+n]) sub B
115df8bae1dSRodney W. Grimes * v = (v[1]v[2]...v[n]) sub B
116df8bae1dSRodney W. Grimes * v[1] != 0
117df8bae1dSRodney W. Grimes * 1 < n <= 4 (if n = 1, we use a different division algorithm)
118df8bae1dSRodney W. Grimes * m >= 0 (otherwise u < v, which we already checked)
119df8bae1dSRodney W. Grimes * m + n = 4
120df8bae1dSRodney W. Grimes * and thus
121df8bae1dSRodney W. Grimes * m = 4 - n <= 2
122df8bae1dSRodney W. Grimes */
123df8bae1dSRodney W. Grimes tmp.uq = uq;
124df8bae1dSRodney W. Grimes u[0] = 0;
125df8bae1dSRodney W. Grimes u[1] = HHALF(tmp.ul[H]);
126df8bae1dSRodney W. Grimes u[2] = LHALF(tmp.ul[H]);
127df8bae1dSRodney W. Grimes u[3] = HHALF(tmp.ul[L]);
128df8bae1dSRodney W. Grimes u[4] = LHALF(tmp.ul[L]);
129df8bae1dSRodney W. Grimes tmp.uq = vq;
130df8bae1dSRodney W. Grimes v[1] = HHALF(tmp.ul[H]);
131df8bae1dSRodney W. Grimes v[2] = LHALF(tmp.ul[H]);
132df8bae1dSRodney W. Grimes v[3] = HHALF(tmp.ul[L]);
133df8bae1dSRodney W. Grimes v[4] = LHALF(tmp.ul[L]);
134df8bae1dSRodney W. Grimes for (n = 4; v[1] == 0; v++) {
135df8bae1dSRodney W. Grimes if (--n == 1) {
136df8bae1dSRodney W. Grimes u_long rbj; /* r*B+u[j] (not root boy jim) */
137df8bae1dSRodney W. Grimes digit q1, q2, q3, q4;
138df8bae1dSRodney W. Grimes
139df8bae1dSRodney W. Grimes /*
140df8bae1dSRodney W. Grimes * Change of plan, per exercise 16.
141df8bae1dSRodney W. Grimes * r = 0;
142df8bae1dSRodney W. Grimes * for j = 1..4:
143df8bae1dSRodney W. Grimes * q[j] = floor((r*B + u[j]) / v),
144df8bae1dSRodney W. Grimes * r = (r*B + u[j]) % v;
145df8bae1dSRodney W. Grimes * We unroll this completely here.
146df8bae1dSRodney W. Grimes */
147df8bae1dSRodney W. Grimes t = v[2]; /* nonzero, by definition */
148df8bae1dSRodney W. Grimes q1 = u[1] / t;
149df8bae1dSRodney W. Grimes rbj = COMBINE(u[1] % t, u[2]);
150df8bae1dSRodney W. Grimes q2 = rbj / t;
151df8bae1dSRodney W. Grimes rbj = COMBINE(rbj % t, u[3]);
152df8bae1dSRodney W. Grimes q3 = rbj / t;
153df8bae1dSRodney W. Grimes rbj = COMBINE(rbj % t, u[4]);
154df8bae1dSRodney W. Grimes q4 = rbj / t;
155df8bae1dSRodney W. Grimes if (arq)
156df8bae1dSRodney W. Grimes *arq = rbj % t;
157df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(q1, q2);
158df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(q3, q4);
159df8bae1dSRodney W. Grimes return (tmp.q);
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes }
162df8bae1dSRodney W. Grimes
163df8bae1dSRodney W. Grimes /*
164df8bae1dSRodney W. Grimes * By adjusting q once we determine m, we can guarantee that
165df8bae1dSRodney W. Grimes * there is a complete four-digit quotient at &qspace[1] when
166df8bae1dSRodney W. Grimes * we finally stop.
167df8bae1dSRodney W. Grimes */
168df8bae1dSRodney W. Grimes for (m = 4 - n; u[1] == 0; u++)
169df8bae1dSRodney W. Grimes m--;
170df8bae1dSRodney W. Grimes for (i = 4 - m; --i >= 0;)
171df8bae1dSRodney W. Grimes q[i] = 0;
172df8bae1dSRodney W. Grimes q += 4 - m;
173df8bae1dSRodney W. Grimes
174df8bae1dSRodney W. Grimes /*
175df8bae1dSRodney W. Grimes * Here we run Program D, translated from MIX to C and acquiring
176df8bae1dSRodney W. Grimes * a few minor changes.
177df8bae1dSRodney W. Grimes *
178df8bae1dSRodney W. Grimes * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
179df8bae1dSRodney W. Grimes */
180df8bae1dSRodney W. Grimes d = 0;
181df8bae1dSRodney W. Grimes for (t = v[1]; t < B / 2; t <<= 1)
182df8bae1dSRodney W. Grimes d++;
183df8bae1dSRodney W. Grimes if (d > 0) {
1846ff5e8a6SAndrew Thompson __shl(&u[0], m + n, d); /* u <<= d */
1856ff5e8a6SAndrew Thompson __shl(&v[1], n - 1, d); /* v <<= d */
186df8bae1dSRodney W. Grimes }
187df8bae1dSRodney W. Grimes /*
188df8bae1dSRodney W. Grimes * D2: j = 0.
189df8bae1dSRodney W. Grimes */
190df8bae1dSRodney W. Grimes j = 0;
191df8bae1dSRodney W. Grimes v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
192df8bae1dSRodney W. Grimes v2 = v[2]; /* for D3 */
193df8bae1dSRodney W. Grimes do {
194484820d4SConrad Meyer digit uj0, uj1, uj2;
195df8bae1dSRodney W. Grimes
196df8bae1dSRodney W. Grimes /*
197df8bae1dSRodney W. Grimes * D3: Calculate qhat (\^q, in TeX notation).
198df8bae1dSRodney W. Grimes * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
199df8bae1dSRodney W. Grimes * let rhat = (u[j]*B + u[j+1]) mod v[1].
200df8bae1dSRodney W. Grimes * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
201df8bae1dSRodney W. Grimes * decrement qhat and increase rhat correspondingly.
202df8bae1dSRodney W. Grimes * Note that if rhat >= B, v[2]*qhat < rhat*B.
203df8bae1dSRodney W. Grimes */
204df8bae1dSRodney W. Grimes uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
205df8bae1dSRodney W. Grimes uj1 = u[j + 1]; /* for D3 only */
206df8bae1dSRodney W. Grimes uj2 = u[j + 2]; /* for D3 only */
207df8bae1dSRodney W. Grimes if (uj0 == v1) {
208df8bae1dSRodney W. Grimes qhat = B;
209df8bae1dSRodney W. Grimes rhat = uj1;
210df8bae1dSRodney W. Grimes goto qhat_too_big;
211df8bae1dSRodney W. Grimes } else {
2120abc78a6SPoul-Henning Kamp u_long nn = COMBINE(uj0, uj1);
2130abc78a6SPoul-Henning Kamp qhat = nn / v1;
2140abc78a6SPoul-Henning Kamp rhat = nn % v1;
215df8bae1dSRodney W. Grimes }
216df8bae1dSRodney W. Grimes while (v2 * qhat > COMBINE(rhat, uj2)) {
217df8bae1dSRodney W. Grimes qhat_too_big:
218df8bae1dSRodney W. Grimes qhat--;
219df8bae1dSRodney W. Grimes if ((rhat += v1) >= B)
220df8bae1dSRodney W. Grimes break;
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes /*
223df8bae1dSRodney W. Grimes * D4: Multiply and subtract.
224df8bae1dSRodney W. Grimes * The variable `t' holds any borrows across the loop.
225df8bae1dSRodney W. Grimes * We split this up so that we do not require v[0] = 0,
226df8bae1dSRodney W. Grimes * and to eliminate a final special case.
227df8bae1dSRodney W. Grimes */
228df8bae1dSRodney W. Grimes for (t = 0, i = n; i > 0; i--) {
229df8bae1dSRodney W. Grimes t = u[i + j] - v[i] * qhat - t;
230df8bae1dSRodney W. Grimes u[i + j] = LHALF(t);
231df8bae1dSRodney W. Grimes t = (B - HHALF(t)) & (B - 1);
232df8bae1dSRodney W. Grimes }
233df8bae1dSRodney W. Grimes t = u[j] - t;
234df8bae1dSRodney W. Grimes u[j] = LHALF(t);
235df8bae1dSRodney W. Grimes /*
236df8bae1dSRodney W. Grimes * D5: test remainder.
237df8bae1dSRodney W. Grimes * There is a borrow if and only if HHALF(t) is nonzero;
238df8bae1dSRodney W. Grimes * in that (rare) case, qhat was too large (by exactly 1).
239df8bae1dSRodney W. Grimes * Fix it by adding v[1..n] to u[j..j+n].
240df8bae1dSRodney W. Grimes */
241df8bae1dSRodney W. Grimes if (HHALF(t)) {
242df8bae1dSRodney W. Grimes qhat--;
243df8bae1dSRodney W. Grimes for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
244df8bae1dSRodney W. Grimes t += u[i + j] + v[i];
245df8bae1dSRodney W. Grimes u[i + j] = LHALF(t);
246df8bae1dSRodney W. Grimes t = HHALF(t);
247df8bae1dSRodney W. Grimes }
248df8bae1dSRodney W. Grimes u[j] = LHALF(u[j] + t);
249df8bae1dSRodney W. Grimes }
250df8bae1dSRodney W. Grimes q[j] = qhat;
251df8bae1dSRodney W. Grimes } while (++j <= m); /* D7: loop on j. */
252df8bae1dSRodney W. Grimes
253df8bae1dSRodney W. Grimes /*
254df8bae1dSRodney W. Grimes * If caller wants the remainder, we have to calculate it as
255df8bae1dSRodney W. Grimes * u[m..m+n] >> d (this is at most n digits and thus fits in
256df8bae1dSRodney W. Grimes * u[m+1..m+n], but we may need more source digits).
257df8bae1dSRodney W. Grimes */
258df8bae1dSRodney W. Grimes if (arq) {
259df8bae1dSRodney W. Grimes if (d) {
260df8bae1dSRodney W. Grimes for (i = m + n; i > m; --i)
261df8bae1dSRodney W. Grimes u[i] = (u[i] >> d) |
262df8bae1dSRodney W. Grimes LHALF(u[i - 1] << (HALF_BITS - d));
263df8bae1dSRodney W. Grimes u[i] = 0;
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
266df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
267df8bae1dSRodney W. Grimes *arq = tmp.q;
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes
270df8bae1dSRodney W. Grimes tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
271df8bae1dSRodney W. Grimes tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
272df8bae1dSRodney W. Grimes return (tmp.q);
273df8bae1dSRodney W. Grimes }
274