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