Add a manual page explaining the format of /etc/manpath.config.
[dragonfly/vkernel-mp.git] / lib / libc / quad / muldi3.c
bloba3552790e2f2dffa30e5c1c9a310aadfd43bff38
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. 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.
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.
37 * @(#)muldi3.c 8.1 (Berkeley) 6/4/93
38 * $DragonFly: src/lib/libc/quad/muldi3.c,v 1.3 2004/10/25 21:38:01 drhodus Exp $
41 #include "quad.h"
44 * Multiply two quads.
46 * Our algorithm is based on the following. Split incoming quad values
47 * u and v (where u,v >= 0) into
49 * u = 2^n u1 * u0 (n = number of bits in `u_long', usu. 32)
51 * and
53 * v = 2^n v1 * v0
55 * Then
57 * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
58 * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
60 * Now add 2^n u1 v1 to the first term and subtract it from the middle,
61 * and add 2^n u0 v0 to the last term and subtract it from the middle.
62 * This gives:
64 * uv = (2^2n + 2^n) (u1 v1) +
65 * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
66 * (2^n + 1) (u0 v0)
68 * Factoring the middle a bit gives us:
70 * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
71 * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
72 * (2^n + 1) (u0 v0) [u0v0 = low]
74 * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
75 * in just half the precision of the original. (Note that either or both
76 * of (u1 - u0) or (v0 - v1) may be negative.)
78 * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
80 * Since C does not give us a `long * long = quad' operator, we split
81 * our input quads into two longs, then split the two longs into two
82 * shorts. We can then calculate `short * short = long' in native
83 * arithmetic.
85 * Our product should, strictly speaking, be a `long quad', with 128
86 * bits, but we are going to discard the upper 64. In other words,
87 * we are not interested in uv, but rather in (uv mod 2^2n). This
88 * makes some of the terms above vanish, and we get:
90 * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
92 * or
94 * (2^n)(high + mid + low) + low
96 * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
97 * of 2^n in either one will also vanish. Only `low' need be computed
98 * mod 2^2n, and only because of the final term above.
100 static quad_t __lmulq(u_long, u_long);
102 quad_t
103 __muldi3(a, b)
104 quad_t a, b;
106 union uu u, v, low, prod;
107 u_long high, mid, udiff, vdiff;
108 int negall, negmid;
109 #define u1 u.ul[H]
110 #define u0 u.ul[L]
111 #define v1 v.ul[H]
112 #define v0 v.ul[L]
115 * Get u and v such that u, v >= 0. When this is finished,
116 * u1, u0, v1, and v0 will be directly accessible through the
117 * longword fields.
119 if (a >= 0)
120 u.q = a, negall = 0;
121 else
122 u.q = -a, negall = 1;
123 if (b >= 0)
124 v.q = b;
125 else
126 v.q = -b, negall ^= 1;
128 if (u1 == 0 && v1 == 0) {
130 * An (I hope) important optimization occurs when u1 and v1
131 * are both 0. This should be common since most numbers
132 * are small. Here the product is just u0*v0.
134 prod.q = __lmulq(u0, v0);
135 } else {
137 * Compute the three intermediate products, remembering
138 * whether the middle term is negative. We can discard
139 * any upper bits in high and mid, so we can use native
140 * u_long * u_long => u_long arithmetic.
142 low.q = __lmulq(u0, v0);
144 if (u1 >= u0)
145 negmid = 0, udiff = u1 - u0;
146 else
147 negmid = 1, udiff = u0 - u1;
148 if (v0 >= v1)
149 vdiff = v0 - v1;
150 else
151 vdiff = v1 - v0, negmid ^= 1;
152 mid = udiff * vdiff;
154 high = u1 * v1;
157 * Assemble the final product.
159 prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
160 low.ul[H];
161 prod.ul[L] = low.ul[L];
163 return (negall ? -prod.q : prod.q);
164 #undef u1
165 #undef u0
166 #undef v1
167 #undef v0
171 * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half
172 * the number of bits in a long (whatever that is---the code below
173 * does not care as long as quad.h does its part of the bargain---but
174 * typically N==16).
176 * We use the same algorithm from Knuth, but this time the modulo refinement
177 * does not apply. On the other hand, since N is half the size of a long,
178 * we can get away with native multiplication---none of our input terms
179 * exceeds (ULONG_MAX >> 1).
181 * Note that, for u_long l, the quad-precision result
183 * l << N
185 * splits into high and low longs as HHALF(l) and LHUP(l) respectively.
187 static quad_t
188 __lmulq(u_long u, u_long v)
190 u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low;
191 u_long prodh, prodl, was;
192 union uu prod;
193 int neg;
195 u1 = HHALF(u);
196 u0 = LHALF(u);
197 v1 = HHALF(v);
198 v0 = LHALF(v);
200 low = u0 * v0;
202 /* This is the same small-number optimization as before. */
203 if (u1 == 0 && v1 == 0)
204 return (low);
206 if (u1 >= u0)
207 udiff = u1 - u0, neg = 0;
208 else
209 udiff = u0 - u1, neg = 1;
210 if (v0 >= v1)
211 vdiff = v0 - v1;
212 else
213 vdiff = v1 - v0, neg ^= 1;
214 mid = udiff * vdiff;
216 high = u1 * v1;
218 /* prod = (high << 2N) + (high << N); */
219 prodh = high + HHALF(high);
220 prodl = LHUP(high);
222 /* if (neg) prod -= mid << N; else prod += mid << N; */
223 if (neg) {
224 was = prodl;
225 prodl -= LHUP(mid);
226 prodh -= HHALF(mid) + (prodl > was);
227 } else {
228 was = prodl;
229 prodl += LHUP(mid);
230 prodh += HHALF(mid) + (prodl < was);
233 /* prod += low << N */
234 was = prodl;
235 prodl += LHUP(low);
236 prodh += HHALF(low) + (prodl < was);
237 /* ... + low; */
238 if ((prodl += low) < low)
239 prodh++;
241 /* return 4N-bit product */
242 prod.ul[H] = prodh;
243 prod.ul[L] = prodl;
244 return (prod.q);