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