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