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