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