xref: /openbsd-src/sys/lib/libkern/muldi3.c (revision 0bca52fc7c89ec4766656ff7feea08eaab3446cb)
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
__muldi3(quad_t a,quad_t b)96 __muldi3(quad_t a, quad_t b)
97 {
98 	union uu u, v, low, prod;
99 	u_int high, mid, udiff, vdiff;
100 	int negall, negmid;
101 #define	u1	u.ul[H]
102 #define	u0	u.ul[L]
103 #define	v1	v.ul[H]
104 #define	v0	v.ul[L]
105 
106 	/*
107 	 * Get u and v such that u, v >= 0.  When this is finished,
108 	 * u1, u0, v1, and v0 will be directly accessible through the
109 	 * int fields.
110 	 */
111 	if (a >= 0)
112 		u.q = a, negall = 0;
113 	else
114 		u.q = -a, negall = 1;
115 	if (b >= 0)
116 		v.q = b;
117 	else
118 		v.q = -b, negall ^= 1;
119 
120 	if (u1 == 0 && v1 == 0) {
121 		/*
122 		 * An (I hope) important optimization occurs when u1 and v1
123 		 * are both 0.  This should be common since most numbers
124 		 * are small.  Here the product is just u0*v0.
125 		 */
126 		prod.q = __lmulq(u0, v0);
127 	} else {
128 		/*
129 		 * Compute the three intermediate products, remembering
130 		 * whether the middle term is negative.  We can discard
131 		 * any upper bits in high and mid, so we can use native
132 		 * u_int * u_int => u_int arithmetic.
133 		 */
134 		low.q = __lmulq(u0, v0);
135 
136 		if (u1 >= u0)
137 			negmid = 0, udiff = u1 - u0;
138 		else
139 			negmid = 1, udiff = u0 - u1;
140 		if (v0 >= v1)
141 			vdiff = v0 - v1;
142 		else
143 			vdiff = v1 - v0, negmid ^= 1;
144 		mid = udiff * vdiff;
145 
146 		high = u1 * v1;
147 
148 		/*
149 		 * Assemble the final product.
150 		 */
151 		prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
152 		    low.ul[H];
153 		prod.ul[L] = low.ul[L];
154 	}
155 	return (negall ? -prod.q : prod.q);
156 #undef u1
157 #undef u0
158 #undef v1
159 #undef v0
160 }
161 
162 /*
163  * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
164  * the number of bits in an int (whatever that is---the code below
165  * does not care as long as quad.h does its part of the bargain---but
166  * typically N==16).
167  *
168  * We use the same algorithm from Knuth, but this time the modulo refinement
169  * does not apply.  On the other hand, since N is half the size of an int,
170  * we can get away with native multiplication---none of our input terms
171  * exceeds (UINT_MAX >> 1).
172  *
173  * Note that, for u_int l, the quad-precision result
174  *
175  *	l << N
176  *
177  * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
178  */
179 static quad_t
__lmulq(u_int u,u_int v)180 __lmulq(u_int u, u_int v)
181 {
182 	u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
183 	u_int prodh, prodl, was;
184 	union uu prod;
185 	int neg;
186 
187 	u1 = HHALF(u);
188 	u0 = LHALF(u);
189 	v1 = HHALF(v);
190 	v0 = LHALF(v);
191 
192 	low = u0 * v0;
193 
194 	/* This is the same small-number optimization as before. */
195 	if (u1 == 0 && v1 == 0)
196 		return (low);
197 
198 	if (u1 >= u0)
199 		udiff = u1 - u0, neg = 0;
200 	else
201 		udiff = u0 - u1, neg = 1;
202 	if (v0 >= v1)
203 		vdiff = v0 - v1;
204 	else
205 		vdiff = v1 - v0, neg ^= 1;
206 	mid = udiff * vdiff;
207 
208 	high = u1 * v1;
209 
210 	/* prod = (high << 2N) + (high << N); */
211 	prodh = high + HHALF(high);
212 	prodl = LHUP(high);
213 
214 	/* if (neg) prod -= mid << N; else prod += mid << N; */
215 	if (neg) {
216 		was = prodl;
217 		prodl -= LHUP(mid);
218 		prodh -= HHALF(mid) + (prodl > was);
219 	} else {
220 		was = prodl;
221 		prodl += LHUP(mid);
222 		prodh += HHALF(mid) + (prodl < was);
223 	}
224 
225 	/* prod += low << N */
226 	was = prodl;
227 	prodl += LHUP(low);
228 	prodh += HHALF(low) + (prodl < was);
229 	/* ... + low; */
230 	if ((prodl += low) < low)
231 		prodh++;
232 
233 	/* return 4N-bit product */
234 	prod.ul[H] = prodh;
235 	prod.ul[L] = prodl;
236 	return (prod.q);
237 }
238