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