Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / sys / libkern / muldi3.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  * 3. 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  * $FreeBSD: src/sys/libkern/muldi3.c,v 1.6 1999/08/28 00:46:34 peter Exp $
34  * $DragonFly: src/sys/libkern/muldi3.c,v 1.4 2004/01/26 11:09:44 joerg Exp $
35  */
36
37 #include "quad.h"
38
39 /*
40  * Multiply two quads.
41  *
42  * Our algorithm is based on the following.  Split incoming quad values
43  * u and v (where u,v >= 0) into
44  *
45  *      u = 2^n u1  *  u0       (n = number of bits in `u_long', usu. 32)
46  *
47  * and
48  *
49  *      v = 2^n v1  *  v0
50  *
51  * Then
52  *
53  *      uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
54  *         = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
55  *
56  * Now add 2^n u1 v1 to the first term and subtract it from the middle,
57  * and add 2^n u0 v0 to the last term and subtract it from the middle.
58  * This gives:
59  *
60  *      uv = (2^2n + 2^n) (u1 v1)  +
61  *               (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
62  *             (2^n + 1)  (u0 v0)
63  *
64  * Factoring the middle a bit gives us:
65  *
66  *      uv = (2^2n + 2^n) (u1 v1)  +                    [u1v1 = high]
67  *               (2^n)    (u1 - u0) (v0 - v1)  +        [(u1-u0)... = mid]
68  *             (2^n + 1)  (u0 v0)                       [u0v0 = low]
69  *
70  * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
71  * in just half the precision of the original.  (Note that either or both
72  * of (u1 - u0) or (v0 - v1) may be negative.)
73  *
74  * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
75  *
76  * Since C does not give us a `long * long = quad' operator, we split
77  * our input quads into two longs, then split the two longs into two
78  * shorts.  We can then calculate `short * short = long' in native
79  * arithmetic.
80  *
81  * Our product should, strictly speaking, be a `long quad', with 128
82  * bits, but we are going to discard the upper 64.  In other words,
83  * we are not interested in uv, but rather in (uv mod 2^2n).  This
84  * makes some of the terms above vanish, and we get:
85  *
86  *      (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
87  *
88  * or
89  *
90  *      (2^n)(high + mid + low) + low
91  *
92  * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
93  * of 2^n in either one will also vanish.  Only `low' need be computed
94  * mod 2^2n, and only because of the final term above.
95  */
96 static quad_t __lmulq(u_long u, u_long v);
97
98 quad_t
99 __muldi3(quad_t a, quad_t b)
100 {
101         union uu u, v, low, prod;
102         u_long high, mid, udiff, vdiff;
103         int negall, negmid;
104 #define u1      u.ul[H]
105 #define u0      u.ul[L]
106 #define v1      v.ul[H]
107 #define v0      v.ul[L]
108
109         /*
110          * Get u and v such that u, v >= 0.  When this is finished,
111          * u1, u0, v1, and v0 will be directly accessible through the
112          * longword fields.
113          */
114         if (a >= 0)
115                 u.q = a, negall = 0;
116         else
117                 u.q = -a, negall = 1;
118         if (b >= 0)
119                 v.q = b;
120         else
121                 v.q = -b, negall ^= 1;
122
123         if (u1 == 0 && v1 == 0) {
124                 /*
125                  * An (I hope) important optimization occurs when u1 and v1
126                  * are both 0.  This should be common since most numbers
127                  * are small.  Here the product is just u0*v0.
128                  */
129                 prod.q = __lmulq(u0, v0);
130         } else {
131                 /*
132                  * Compute the three intermediate products, remembering
133                  * whether the middle term is negative.  We can discard
134                  * any upper bits in high and mid, so we can use native
135                  * u_long * u_long => u_long arithmetic.
136                  */
137                 low.q = __lmulq(u0, v0);
138
139                 if (u1 >= u0)
140                         negmid = 0, udiff = u1 - u0;
141                 else
142                         negmid = 1, udiff = u0 - u1;
143                 if (v0 >= v1)
144                         vdiff = v0 - v1;
145                 else
146                         vdiff = v1 - v0, negmid ^= 1;
147                 mid = udiff * vdiff;
148
149                 high = u1 * v1;
150
151                 /*
152                  * Assemble the final product.
153                  */
154                 prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
155                     low.ul[H];
156                 prod.ul[L] = low.ul[L];
157         }
158         return (negall ? -prod.q : prod.q);
159 #undef u1
160 #undef u0
161 #undef v1
162 #undef v0
163 }
164
165 /*
166  * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
167  * the number of bits in a long (whatever that is---the code below
168  * does not care as long as quad.h does its part of the bargain---but
169  * typically N==16).
170  *
171  * We use the same algorithm from Knuth, but this time the modulo refinement
172  * does not apply.  On the other hand, since N is half the size of a long,
173  * we can get away with native multiplication---none of our input terms
174  * exceeds (ULONG_MAX >> 1).
175  *
176  * Note that, for u_long l, the quad-precision result
177  *
178  *      l << N
179  *
180  * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
181  */
182 static quad_t
183 __lmulq(u_long u, u_long v)
184 {
185         u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
186         u_long prodh, prodl, was;
187         union uu prod;
188         int neg;
189
190         u1 = HHALF(u);
191         u0 = LHALF(u);
192         v1 = HHALF(v);
193         v0 = LHALF(v);
194
195         low = u0 * v0;
196
197         /* This is the same small-number optimization as before. */
198         if (u1 == 0 && v1 == 0)
199                 return (low);
200
201         if (u1 >= u0)
202                 udiff = u1 - u0, neg = 0;
203         else
204                 udiff = u0 - u1, neg = 1;
205         if (v0 >= v1)
206                 vdiff = v0 - v1;
207         else
208                 vdiff = v1 - v0, neg ^= 1;
209         mid = udiff * vdiff;
210
211         high = u1 * v1;
212
213         /* prod = (high << 2N) + (high << N); */
214         prodh = high + HHALF(high);
215         prodl = LHUP(high);
216
217         /* if (neg) prod -= mid << N; else prod += mid << N; */
218         if (neg) {
219                 was = prodl;
220                 prodl -= LHUP(mid);
221                 prodh -= HHALF(mid) + (prodl > was);
222         } else {
223                 was = prodl;
224                 prodl += LHUP(mid);
225                 prodh += HHALF(mid) + (prodl < was);
226         }
227
228         /* prod += low << N */
229         was = prodl;
230         prodl += LHUP(low);
231         prodh += HHALF(low) + (prodl < was);
232         /* ... + low; */
233         if ((prodl += low) < low)
234                 prodh++;
235
236         /* return 4N-bit product */
237         prod.ul[H] = prodh;
238         prod.ul[L] = prodl;
239         return (prod.q);
240 }