1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright (c) 1992, 1993
3*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
4*0Sstevel@tonic-gate *
5*0Sstevel@tonic-gate * This software was developed by the Computer Systems Engineering group
6*0Sstevel@tonic-gate * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7*0Sstevel@tonic-gate * contributed to Berkeley.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
10*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
11*0Sstevel@tonic-gate * are met:
12*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
15*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
16*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
17*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
18*0Sstevel@tonic-gate * must display the following acknowledgement:
19*0Sstevel@tonic-gate * This product includes software developed by the University of
20*0Sstevel@tonic-gate * California, Berkeley and its contributors.
21*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
22*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
23*0Sstevel@tonic-gate * without specific prior written permission.
24*0Sstevel@tonic-gate *
25*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*0Sstevel@tonic-gate * SUCH DAMAGE.
36*0Sstevel@tonic-gate */
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #include "quadint.h"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #pragma weak __muldi3 = ___muldi3
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate * Multiply two quads.
46*0Sstevel@tonic-gate *
47*0Sstevel@tonic-gate * Our algorithm is based on the following. Split incoming quad values
48*0Sstevel@tonic-gate * u and v (where u,v >= 0) into
49*0Sstevel@tonic-gate *
50*0Sstevel@tonic-gate * u = 2^n u1 * u0 (n = number of bits in `u_long', usu. 32)
51*0Sstevel@tonic-gate *
52*0Sstevel@tonic-gate * and
53*0Sstevel@tonic-gate *
54*0Sstevel@tonic-gate * v = 2^n v1 * v0
55*0Sstevel@tonic-gate *
56*0Sstevel@tonic-gate * Then
57*0Sstevel@tonic-gate *
58*0Sstevel@tonic-gate * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
59*0Sstevel@tonic-gate * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
60*0Sstevel@tonic-gate *
61*0Sstevel@tonic-gate * Now add 2^n u1 v1 to the first term and subtract it from the middle,
62*0Sstevel@tonic-gate * and add 2^n u0 v0 to the last term and subtract it from the middle.
63*0Sstevel@tonic-gate * This gives:
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * uv = (2^2n + 2^n) (u1 v1) +
66*0Sstevel@tonic-gate * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
67*0Sstevel@tonic-gate * (2^n + 1) (u0 v0)
68*0Sstevel@tonic-gate *
69*0Sstevel@tonic-gate * Factoring the middle a bit gives us:
70*0Sstevel@tonic-gate *
71*0Sstevel@tonic-gate * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
72*0Sstevel@tonic-gate * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
73*0Sstevel@tonic-gate * (2^n + 1) (u0 v0) [u0v0 = low]
74*0Sstevel@tonic-gate *
75*0Sstevel@tonic-gate * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
76*0Sstevel@tonic-gate * in just half the precision of the original. (Note that either or both
77*0Sstevel@tonic-gate * of (u1 - u0) or (v0 - v1) may be negative.)
78*0Sstevel@tonic-gate *
79*0Sstevel@tonic-gate * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
80*0Sstevel@tonic-gate *
81*0Sstevel@tonic-gate * Since C does not give us a `long * long = quad' operator, we split
82*0Sstevel@tonic-gate * our input quads into two longs, then split the two longs into two
83*0Sstevel@tonic-gate * shorts. We can then calculate `short * short = long' in native
84*0Sstevel@tonic-gate * arithmetic.
85*0Sstevel@tonic-gate *
86*0Sstevel@tonic-gate * Our product should, strictly speaking, be a `long quad', with 128
87*0Sstevel@tonic-gate * bits, but we are going to discard the upper 64. In other words,
88*0Sstevel@tonic-gate * we are not interested in uv, but rather in (uv mod 2^2n). This
89*0Sstevel@tonic-gate * makes some of the terms above vanish, and we get:
90*0Sstevel@tonic-gate *
91*0Sstevel@tonic-gate * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
92*0Sstevel@tonic-gate *
93*0Sstevel@tonic-gate * or
94*0Sstevel@tonic-gate *
95*0Sstevel@tonic-gate * (2^n)(high + mid + low) + low
96*0Sstevel@tonic-gate *
97*0Sstevel@tonic-gate * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
98*0Sstevel@tonic-gate * of 2^n in either one will also vanish. Only `low' need be computed
99*0Sstevel@tonic-gate * mod 2^2n, and only because of the final term above.
100*0Sstevel@tonic-gate */
101*0Sstevel@tonic-gate static longlong_t __lmulq(ulong_t, ulong_t);
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate longlong_t
___muldi3(longlong_t a,longlong_t b)104*0Sstevel@tonic-gate ___muldi3(longlong_t a, longlong_t b)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate union uu u, v, low, prod;
107*0Sstevel@tonic-gate ulong_t high, mid, udiff, vdiff;
108*0Sstevel@tonic-gate int negall, negmid;
109*0Sstevel@tonic-gate #define u1 u.ul[H]
110*0Sstevel@tonic-gate #define u0 u.ul[L]
111*0Sstevel@tonic-gate #define v1 v.ul[H]
112*0Sstevel@tonic-gate #define v0 v.ul[L]
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * Get u and v such that u, v >= 0. When this is finished,
116*0Sstevel@tonic-gate * u1, u0, v1, and v0 will be directly accessible through the
117*0Sstevel@tonic-gate * longword fields.
118*0Sstevel@tonic-gate */
119*0Sstevel@tonic-gate if (a >= 0)
120*0Sstevel@tonic-gate u.q = a, negall = 0;
121*0Sstevel@tonic-gate else
122*0Sstevel@tonic-gate u.q = -a, negall = 1;
123*0Sstevel@tonic-gate if (b >= 0)
124*0Sstevel@tonic-gate v.q = b;
125*0Sstevel@tonic-gate else
126*0Sstevel@tonic-gate v.q = -b, negall ^= 1;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate if (u1 == 0 && v1 == 0) {
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * An (I hope) important optimization occurs when u1 and v1
131*0Sstevel@tonic-gate * are both 0. This should be common since most numbers
132*0Sstevel@tonic-gate * are small. Here the product is just u0*v0.
133*0Sstevel@tonic-gate */
134*0Sstevel@tonic-gate prod.q = __lmulq(u0, v0);
135*0Sstevel@tonic-gate } else {
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate * Compute the three intermediate products, remembering
138*0Sstevel@tonic-gate * whether the middle term is negative. We can discard
139*0Sstevel@tonic-gate * any upper bits in high and mid, so we can use native
140*0Sstevel@tonic-gate * ulong_t * ulong_t => ulong_t arithmetic.
141*0Sstevel@tonic-gate */
142*0Sstevel@tonic-gate low.q = __lmulq(u0, v0);
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate if (u1 >= u0)
145*0Sstevel@tonic-gate negmid = 0, udiff = u1 - u0;
146*0Sstevel@tonic-gate else
147*0Sstevel@tonic-gate negmid = 1, udiff = u0 - u1;
148*0Sstevel@tonic-gate if (v0 >= v1)
149*0Sstevel@tonic-gate vdiff = v0 - v1;
150*0Sstevel@tonic-gate else
151*0Sstevel@tonic-gate vdiff = v1 - v0, negmid ^= 1;
152*0Sstevel@tonic-gate mid = udiff * vdiff;
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate high = u1 * v1;
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate /*
157*0Sstevel@tonic-gate * Assemble the final product.
158*0Sstevel@tonic-gate */
159*0Sstevel@tonic-gate prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
160*0Sstevel@tonic-gate low.ul[H];
161*0Sstevel@tonic-gate prod.ul[L] = low.ul[L];
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate return (negall ? -prod.q : prod.q);
164*0Sstevel@tonic-gate #undef u1
165*0Sstevel@tonic-gate #undef u0
166*0Sstevel@tonic-gate #undef v1
167*0Sstevel@tonic-gate #undef v0
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
172*0Sstevel@tonic-gate * the number of bits in a long (whatever that is---the code below
173*0Sstevel@tonic-gate * does not care as long as quad.h does its part of the bargain---but
174*0Sstevel@tonic-gate * typically N==16).
175*0Sstevel@tonic-gate *
176*0Sstevel@tonic-gate * We use the same algorithm from Knuth, but this time the modulo refinement
177*0Sstevel@tonic-gate * does not apply. On the other hand, since N is half the size of a long,
178*0Sstevel@tonic-gate * we can get away with native multiplication---none of our input terms
179*0Sstevel@tonic-gate * exceeds (ULONG_MAX >> 1).
180*0Sstevel@tonic-gate *
181*0Sstevel@tonic-gate * Note that, for ulong_t l, the quad-precision result
182*0Sstevel@tonic-gate *
183*0Sstevel@tonic-gate * l << N
184*0Sstevel@tonic-gate *
185*0Sstevel@tonic-gate * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
186*0Sstevel@tonic-gate */
187*0Sstevel@tonic-gate static longlong_t
__lmulq(ulong_t u,ulong_t v)188*0Sstevel@tonic-gate __lmulq(ulong_t u, ulong_t v)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate ulong_t u1, u0, v1, v0, udiff, vdiff, high, mid, low;
191*0Sstevel@tonic-gate ulong_t prodh, prodl, was;
192*0Sstevel@tonic-gate union uu prod;
193*0Sstevel@tonic-gate int neg;
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate u1 = HHALF(u);
196*0Sstevel@tonic-gate u0 = LHALF(u);
197*0Sstevel@tonic-gate v1 = HHALF(v);
198*0Sstevel@tonic-gate v0 = LHALF(v);
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate low = u0 * v0;
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate /* This is the same small-number optimization as before. */
203*0Sstevel@tonic-gate if (u1 == 0 && v1 == 0)
204*0Sstevel@tonic-gate return (low);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate if (u1 >= u0)
207*0Sstevel@tonic-gate udiff = u1 - u0, neg = 0;
208*0Sstevel@tonic-gate else
209*0Sstevel@tonic-gate udiff = u0 - u1, neg = 1;
210*0Sstevel@tonic-gate if (v0 >= v1)
211*0Sstevel@tonic-gate vdiff = v0 - v1;
212*0Sstevel@tonic-gate else
213*0Sstevel@tonic-gate vdiff = v1 - v0, neg ^= 1;
214*0Sstevel@tonic-gate mid = udiff * vdiff;
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate high = u1 * v1;
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate /* prod = (high << 2N) + (high << N); */
219*0Sstevel@tonic-gate prodh = high + HHALF(high);
220*0Sstevel@tonic-gate prodl = LHUP(high);
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /* if (neg) prod -= mid << N; else prod += mid << N; */
223*0Sstevel@tonic-gate if (neg) {
224*0Sstevel@tonic-gate was = prodl;
225*0Sstevel@tonic-gate prodl -= LHUP(mid);
226*0Sstevel@tonic-gate prodh -= HHALF(mid) + (prodl > was);
227*0Sstevel@tonic-gate } else {
228*0Sstevel@tonic-gate was = prodl;
229*0Sstevel@tonic-gate prodl += LHUP(mid);
230*0Sstevel@tonic-gate prodh += HHALF(mid) + (prodl < was);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate /* prod += low << N */
234*0Sstevel@tonic-gate was = prodl;
235*0Sstevel@tonic-gate prodl += LHUP(low);
236*0Sstevel@tonic-gate prodh += HHALF(low) + (prodl < was);
237*0Sstevel@tonic-gate /* ... + low; */
238*0Sstevel@tonic-gate if ((prodl += low) < low)
239*0Sstevel@tonic-gate prodh++;
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate /* return 4N-bit product */
242*0Sstevel@tonic-gate prod.ul[H] = prodh;
243*0Sstevel@tonic-gate prod.ul[L] = prodl;
244*0Sstevel@tonic-gate return (prod.q);
245*0Sstevel@tonic-gate }
246