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