]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libstand/qdivrem.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libstand / qdivrem.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      From: Id: qdivrem.c,v 1.7 1997/11/07 09:20:40 phk Exp
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
41  * section 4.3.1, pp. 257--259.
42  */
43
44 #include "quad.h"
45
46 #define B       (1 << HALF_BITS)        /* digit base */
47
48 /* Combine two `digits' to make a single two-digit number. */
49 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
50
51 /* select a type for digits in base B: use unsigned short if they fit */
52 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
53 typedef unsigned short digit;
54 #else
55 typedef u_long digit;
56 #endif
57
58 /*
59  * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
60  * `fall out' the left (there never will be any such anyway).
61  * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
62  */
63 static void
64 shl(digit *p, int len, int sh)
65 {
66         int i;
67
68         for (i = 0; i < len; i++)
69                 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
70         p[i] = LHALF(p[i] << sh);
71 }
72
73 /*
74  * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
75  *
76  * We do this in base 2-sup-HALF_BITS, so that all intermediate products
77  * fit within u_long.  As a consequence, the maximum length dividend and
78  * divisor are 4 `digits' in this base (they are shorter if they have
79  * leading zeros).
80  */
81 u_quad_t
82 __qdivrem(uq, vq, arq)
83         u_quad_t uq, vq, *arq;
84 {
85         union uu tmp;
86         digit *u, *v, *q;
87         digit v1, v2;
88         u_long qhat, rhat, t;
89         int m, n, d, j, i;
90         digit uspace[5], vspace[5], qspace[5];
91
92         /*
93          * Take care of special cases: divide by zero, and u < v.
94          */
95         if (vq == 0) {
96                 /* divide by zero. */
97                 static volatile const unsigned int zero = 0;
98
99                 tmp.ul[H] = tmp.ul[L] = 1 / zero;
100                 if (arq)
101                         *arq = uq;
102                 return (tmp.q);
103         }
104         if (uq < vq) {
105                 if (arq)
106                         *arq = uq;
107                 return (0);
108         }
109         u = &uspace[0];
110         v = &vspace[0];
111         q = &qspace[0];
112
113         /*
114          * Break dividend and divisor into digits in base B, then
115          * count leading zeros to determine m and n.  When done, we
116          * will have:
117          *      u = (u[1]u[2]...u[m+n]) sub B
118          *      v = (v[1]v[2]...v[n]) sub B
119          *      v[1] != 0
120          *      1 < n <= 4 (if n = 1, we use a different division algorithm)
121          *      m >= 0 (otherwise u < v, which we already checked)
122          *      m + n = 4
123          * and thus
124          *      m = 4 - n <= 2
125          */
126         tmp.uq = uq;
127         u[0] = 0;
128         u[1] = HHALF(tmp.ul[H]);
129         u[2] = LHALF(tmp.ul[H]);
130         u[3] = HHALF(tmp.ul[L]);
131         u[4] = LHALF(tmp.ul[L]);
132         tmp.uq = vq;
133         v[1] = HHALF(tmp.ul[H]);
134         v[2] = LHALF(tmp.ul[H]);
135         v[3] = HHALF(tmp.ul[L]);
136         v[4] = LHALF(tmp.ul[L]);
137         for (n = 4; v[1] == 0; v++) {
138                 if (--n == 1) {
139                         u_long rbj;     /* r*B+u[j] (not root boy jim) */
140                         digit q1, q2, q3, q4;
141
142                         /*
143                          * Change of plan, per exercise 16.
144                          *      r = 0;
145                          *      for j = 1..4:
146                          *              q[j] = floor((r*B + u[j]) / v),
147                          *              r = (r*B + u[j]) % v;
148                          * We unroll this completely here.
149                          */
150                         t = v[2];       /* nonzero, by definition */
151                         q1 = u[1] / t;
152                         rbj = COMBINE(u[1] % t, u[2]);
153                         q2 = rbj / t;
154                         rbj = COMBINE(rbj % t, u[3]);
155                         q3 = rbj / t;
156                         rbj = COMBINE(rbj % t, u[4]);
157                         q4 = rbj / t;
158                         if (arq)
159                                 *arq = rbj % t;
160                         tmp.ul[H] = COMBINE(q1, q2);
161                         tmp.ul[L] = COMBINE(q3, q4);
162                         return (tmp.q);
163                 }
164         }
165
166         /*
167          * By adjusting q once we determine m, we can guarantee that
168          * there is a complete four-digit quotient at &qspace[1] when
169          * we finally stop.
170          */
171         for (m = 4 - n; u[1] == 0; u++)
172                 m--;
173         for (i = 4 - m; --i >= 0;)
174                 q[i] = 0;
175         q += 4 - m;
176
177         /*
178          * Here we run Program D, translated from MIX to C and acquiring
179          * a few minor changes.
180          *
181          * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
182          */
183         d = 0;
184         for (t = v[1]; t < B / 2; t <<= 1)
185                 d++;
186         if (d > 0) {
187                 shl(&u[0], m + n, d);           /* u <<= d */
188                 shl(&v[1], n - 1, d);           /* v <<= d */
189         }
190         /*
191          * D2: j = 0.
192          */
193         j = 0;
194         v1 = v[1];      /* for D3 -- note that v[1..n] are constant */
195         v2 = v[2];      /* for D3 */
196         do {
197                 digit uj0, uj1, uj2;
198
199                 /*
200                  * D3: Calculate qhat (\^q, in TeX notation).
201                  * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
202                  * let rhat = (u[j]*B + u[j+1]) mod v[1].
203                  * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
204                  * decrement qhat and increase rhat correspondingly.
205                  * Note that if rhat >= B, v[2]*qhat < rhat*B.
206                  */
207                 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
208                 uj1 = u[j + 1]; /* for D3 only */
209                 uj2 = u[j + 2]; /* for D3 only */
210                 if (uj0 == v1) {
211                         qhat = B;
212                         rhat = uj1;
213                         goto qhat_too_big;
214                 } else {
215                         u_long nn = COMBINE(uj0, uj1);
216                         qhat = nn / v1;
217                         rhat = nn % v1;
218                 }
219                 while (v2 * qhat > COMBINE(rhat, uj2)) {
220         qhat_too_big:
221                         qhat--;
222                         if ((rhat += v1) >= B)
223                                 break;
224                 }
225                 /*
226                  * D4: Multiply and subtract.
227                  * The variable `t' holds any borrows across the loop.
228                  * We split this up so that we do not require v[0] = 0,
229                  * and to eliminate a final special case.
230                  */
231                 for (t = 0, i = n; i > 0; i--) {
232                         t = u[i + j] - v[i] * qhat - t;
233                         u[i + j] = LHALF(t);
234                         t = (B - HHALF(t)) & (B - 1);
235                 }
236                 t = u[j] - t;
237                 u[j] = LHALF(t);
238                 /*
239                  * D5: test remainder.
240                  * There is a borrow if and only if HHALF(t) is nonzero;
241                  * in that (rare) case, qhat was too large (by exactly 1).
242                  * Fix it by adding v[1..n] to u[j..j+n].
243                  */
244                 if (HHALF(t)) {
245                         qhat--;
246                         for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
247                                 t += u[i + j] + v[i];
248                                 u[i + j] = LHALF(t);
249                                 t = HHALF(t);
250                         }
251                         u[j] = LHALF(u[j] + t);
252                 }
253                 q[j] = qhat;
254         } while (++j <= m);             /* D7: loop on j. */
255
256         /*
257          * If caller wants the remainder, we have to calculate it as
258          * u[m..m+n] >> d (this is at most n digits and thus fits in
259          * u[m+1..m+n], but we may need more source digits).
260          */
261         if (arq) {
262                 if (d) {
263                         for (i = m + n; i > m; --i)
264                                 u[i] = (u[i] >> d) |
265                                     LHALF(u[i - 1] << (HALF_BITS - d));
266                         u[i] = 0;
267                 }
268                 tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
269                 tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
270                 *arq = tmp.q;
271         }
272
273         tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
274         tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
275         return (tmp.q);
276 }
277
278 /*
279  * Divide two unsigned quads.
280  */
281
282 u_quad_t
283 __udivdi3(a, b)
284         u_quad_t a, b;
285 {
286
287         return (__qdivrem(a, b, (u_quad_t *)0));
288 }
289
290 /*
291  * Return remainder after dividing two unsigned quads.
292  */
293 u_quad_t
294 __umoddi3(a, b)
295         u_quad_t a, b;
296 {
297         u_quad_t r;
298
299         (void)__qdivrem(a, b, &r);
300         return (r);
301 }
302
303 /*
304  * Divide two signed quads.
305  * ??? if -1/2 should produce -1 on this machine, this code is wrong
306  */
307 quad_t
308 __divdi3(a, b)
309         quad_t a, b;
310 {
311         u_quad_t ua, ub, uq;
312         int neg;
313
314         if (a < 0)
315                 ua = -(u_quad_t)a, neg = 1;
316         else
317                 ua = a, neg = 0;
318         if (b < 0)
319                 ub = -(u_quad_t)b, neg ^= 1;
320         else
321                 ub = b;
322         uq = __qdivrem(ua, ub, (u_quad_t *)0);
323         return (neg ? -uq : uq);
324 }
325
326 /*
327  * Return remainder after dividing two signed quads.
328  *
329  * XXX
330  * If -1/2 should produce -1 on this machine, this code is wrong.
331  */
332 quad_t
333 __moddi3(a, b)
334         quad_t a, b;
335 {
336         u_quad_t ua, ub, ur;
337         int neg;
338
339         if (a < 0)
340                 ua = -(u_quad_t)a, neg = 1;
341         else
342                 ua = a, neg = 0;
343         if (b < 0)
344                 ub = -(u_quad_t)b;
345         else
346                 ub = b;
347         (void)__qdivrem(ua, ub, &ur);
348         return (neg ? -ur : ur);
349 }