xref: /freebsd/sys/libkern/arm/muldi3.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*	$NetBSD: muldi3.c,v 1.8 2003/08/07 16:32:09 agc Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. 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 <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)muldi3.c	8.1 (Berkeley) 6/4/93";
42 #else
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45 
46 #include <libkern/quad.h>
47 
48 /*
49  * Multiply two quads.
50  *
51  * Our algorithm is based on the following.  Split incoming quad values
52  * u and v (where u,v >= 0) into
53  *
54  *	u = 2^n u1  *  u0	(n = number of bits in `u_int', usu. 32)
55  *
56  * and
57  *
58  *	v = 2^n v1  *  v0
59  *
60  * Then
61  *
62  *	uv = 2^2n u1 v1  +  2^n u1 v0  +  2^n v1 u0  +  u0 v0
63  *	   = 2^2n u1 v1  +     2^n (u1 v0 + v1 u0)   +  u0 v0
64  *
65  * Now add 2^n u1 v1 to the first term and subtract it from the middle,
66  * and add 2^n u0 v0 to the last term and subtract it from the middle.
67  * This gives:
68  *
69  *	uv = (2^2n + 2^n) (u1 v1)  +
70  *	         (2^n)    (u1 v0 - u1 v1 + u0 v1 - u0 v0)  +
71  *	       (2^n + 1)  (u0 v0)
72  *
73  * Factoring the middle a bit gives us:
74  *
75  *	uv = (2^2n + 2^n) (u1 v1)  +			[u1v1 = high]
76  *		 (2^n)    (u1 - u0) (v0 - v1)  +	[(u1-u0)... = mid]
77  *	       (2^n + 1)  (u0 v0)			[u0v0 = low]
78  *
79  * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
80  * in just half the precision of the original.  (Note that either or both
81  * of (u1 - u0) or (v0 - v1) may be negative.)
82  *
83  * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
84  *
85  * Since C does not give us a `int * int = quad' operator, we split
86  * our input quads into two ints, then split the two ints into two
87  * shorts.  We can then calculate `short * short = int' in native
88  * arithmetic.
89  *
90  * Our product should, strictly speaking, be a `long quad', with 128
91  * bits, but we are going to discard the upper 64.  In other words,
92  * we are not interested in uv, but rather in (uv mod 2^2n).  This
93  * makes some of the terms above vanish, and we get:
94  *
95  *	(2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
96  *
97  * or
98  *
99  *	(2^n)(high + mid + low) + low
100  *
101  * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
102  * of 2^n in either one will also vanish.  Only `low' need be computed
103  * mod 2^2n, and only because of the final term above.
104  */
105 static quad_t __lmulq(u_int, u_int);
106 
107 quad_t __muldi3(quad_t, quad_t);
108 quad_t
109 __muldi3(quad_t a, quad_t b)
110 {
111 	union uu u, v, low, prod;
112 	u_int high, mid, udiff, vdiff;
113 	int negall, negmid;
114 #define	u1	u.ul[H]
115 #define	u0	u.ul[L]
116 #define	v1	v.ul[H]
117 #define	v0	v.ul[L]
118 
119 	/*
120 	 * Get u and v such that u, v >= 0.  When this is finished,
121 	 * u1, u0, v1, and v0 will be directly accessible through the
122 	 * int fields.
123 	 */
124 	if (a >= 0)
125 		u.q = a, negall = 0;
126 	else
127 		u.q = -a, negall = 1;
128 	if (b >= 0)
129 		v.q = b;
130 	else
131 		v.q = -b, negall ^= 1;
132 
133 	if (u1 == 0 && v1 == 0) {
134 		/*
135 		 * An (I hope) important optimization occurs when u1 and v1
136 		 * are both 0.  This should be common since most numbers
137 		 * are small.  Here the product is just u0*v0.
138 		 */
139 		prod.q = __lmulq(u0, v0);
140 	} else {
141 		/*
142 		 * Compute the three intermediate products, remembering
143 		 * whether the middle term is negative.  We can discard
144 		 * any upper bits in high and mid, so we can use native
145 		 * u_int * u_int => u_int arithmetic.
146 		 */
147 		low.q = __lmulq(u0, v0);
148 
149 		if (u1 >= u0)
150 			negmid = 0, udiff = u1 - u0;
151 		else
152 			negmid = 1, udiff = u0 - u1;
153 		if (v0 >= v1)
154 			vdiff = v0 - v1;
155 		else
156 			vdiff = v1 - v0, negmid ^= 1;
157 		mid = udiff * vdiff;
158 
159 		high = u1 * v1;
160 
161 		/*
162 		 * Assemble the final product.
163 		 */
164 		prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
165 		    low.ul[H];
166 		prod.ul[L] = low.ul[L];
167 	}
168 	return (negall ? -prod.q : prod.q);
169 #undef u1
170 #undef u0
171 #undef v1
172 #undef v0
173 }
174 
175 /*
176  * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
177  * the number of bits in an int (whatever that is---the code below
178  * does not care as long as quad.h does its part of the bargain---but
179  * typically N==16).
180  *
181  * We use the same algorithm from Knuth, but this time the modulo refinement
182  * does not apply.  On the other hand, since N is half the size of an int,
183  * we can get away with native multiplication---none of our input terms
184  * exceeds (UINT_MAX >> 1).
185  *
186  * Note that, for u_int l, the quad-precision result
187  *
188  *	l << N
189  *
190  * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
191  */
192 static quad_t
193 __lmulq(u_int u, u_int v)
194 {
195 	u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
196 	u_int prodh, prodl, was;
197 	union uu prod;
198 	int neg;
199 
200 	u1 = HHALF(u);
201 	u0 = LHALF(u);
202 	v1 = HHALF(v);
203 	v0 = LHALF(v);
204 
205 	low = u0 * v0;
206 
207 	/* This is the same small-number optimization as before. */
208 	if (u1 == 0 && v1 == 0)
209 		return (low);
210 
211 	if (u1 >= u0)
212 		udiff = u1 - u0, neg = 0;
213 	else
214 		udiff = u0 - u1, neg = 1;
215 	if (v0 >= v1)
216 		vdiff = v0 - v1;
217 	else
218 		vdiff = v1 - v0, neg ^= 1;
219 	mid = udiff * vdiff;
220 
221 	high = u1 * v1;
222 
223 	/* prod = (high << 2N) + (high << N); */
224 	prodh = high + HHALF(high);
225 	prodl = LHUP(high);
226 
227 	/* if (neg) prod -= mid << N; else prod += mid << N; */
228 	if (neg) {
229 		was = prodl;
230 		prodl -= LHUP(mid);
231 		prodh -= HHALF(mid) + (prodl > was);
232 	} else {
233 		was = prodl;
234 		prodl += LHUP(mid);
235 		prodh += HHALF(mid) + (prodl < was);
236 	}
237 
238 	/* prod += low << N */
239 	was = prodl;
240 	prodl += LHUP(low);
241 	prodh += HHALF(low) + (prodl < was);
242 	/* ... + low; */
243 	if ((prodl += low) < low)
244 		prodh++;
245 
246 	/* return 4N-bit product */
247 	prod.ul[H] = prodh;
248 	prod.ul[L] = prodl;
249 	return (prod.q);
250 }
251