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