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