1 /* $NetBSD: muldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)muldi3.c 8.1 (Berkeley) 6/4/93";
41 __RCSID("$NetBSD: muldi3.c,v 1.1 2005/12/20 19:28:51 christos Exp $");
43 #endif /* LIBC_SCCS and not lint */
50 * Our algorithm is based on the following. Split incoming quad values
51 * u and v (where u,v >= 0) into
53 * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32)
61 * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
62 * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
64 * Now add 2^n u1 v1 to the first term and subtract it from the middle,
65 * and add 2^n u0 v0 to the last term and subtract it from the middle.
68 * uv = (2^2n + 2^n) (u1 v1) +
69 * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
72 * Factoring the middle a bit gives us:
74 * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
75 * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
76 * (2^n + 1) (u0 v0) [u0v0 = low]
78 * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
79 * in just half the precision of the original. (Note that either or both
80 * of (u1 - u0) or (v0 - v1) may be negative.)
82 * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
84 * Since C does not give us a `int * int = quad' operator, we split
85 * our input quads into two ints, then split the two ints into two
86 * shorts. We can then calculate `short * short = int' in native
89 * Our product should, strictly speaking, be a `long quad', with 128
90 * bits, but we are going to discard the upper 64. In other words,
91 * we are not interested in uv, but rather in (uv mod 2^2n). This
92 * makes some of the terms above vanish, and we get:
94 * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
98 * (2^n)(high + mid + low) + low
100 * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
101 * of 2^n in either one will also vanish. Only `low' need be computed
102 * mod 2^2n, and only because of the final term above.
104 static quad_t
__lmulq(u_int
, u_int
);
107 __muldi3(quad_t a
, quad_t b
)
109 union uu u
, v
, low
, prod
;
110 u_int high
, mid
, udiff
, vdiff
;
118 * Get u and v such that u, v >= 0. When this is finished,
119 * u1, u0, v1, and v0 will be directly accessible through the
125 u
.q
= -a
, negall
= 1;
129 v
.q
= -b
, negall
^= 1;
131 if (u1
== 0 && v1
== 0) {
133 * An (I hope) important optimization occurs when u1 and v1
134 * are both 0. This should be common since most numbers
135 * are small. Here the product is just u0*v0.
137 prod
.q
= __lmulq(u0
, v0
);
140 * Compute the three intermediate products, remembering
141 * whether the middle term is negative. We can discard
142 * any upper bits in high and mid, so we can use native
143 * u_int * u_int => u_int arithmetic.
145 low
.q
= __lmulq(u0
, v0
);
148 negmid
= 0, udiff
= u1
- u0
;
150 negmid
= 1, udiff
= u0
- u1
;
154 vdiff
= v1
- v0
, negmid
^= 1;
160 * Assemble the final product.
162 prod
.ul
[H
] = high
+ (negmid
? -mid
: mid
) + low
.ul
[L
] +
164 prod
.ul
[L
] = low
.ul
[L
];
166 return (negall
? -prod
.q
: prod
.q
);
174 * Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
175 * the number of bits in an int (whatever that is---the code below
176 * does not care as long as quad.h does its part of the bargain---but
179 * We use the same algorithm from Knuth, but this time the modulo refinement
180 * does not apply. On the other hand, since N is half the size of an int,
181 * we can get away with native multiplication---none of our input terms
182 * exceeds (UINT_MAX >> 1).
184 * Note that, for u_int l, the quad-precision result
188 * splits into high and low ints as HHALF(l) and LHUP(l) respectively.
191 __lmulq(u_int u
, u_int v
)
193 u_int u1
, u0
, v1
, v0
, udiff
, vdiff
, high
, mid
, low
;
194 u_int prodh
, prodl
, was
;
205 /* This is the same small-number optimization as before. */
206 if (u1
== 0 && v1
== 0)
210 udiff
= u1
- u0
, neg
= 0;
212 udiff
= u0
- u1
, neg
= 1;
216 vdiff
= v1
- v0
, neg
^= 1;
221 /* prod = (high << 2N) + (high << N); */
222 prodh
= high
+ HHALF(high
);
225 /* if (neg) prod -= mid << N; else prod += mid << N; */
229 prodh
-= HHALF(mid
) + (prodl
> was
);
233 prodh
+= HHALF(mid
) + (prodl
< was
);
236 /* prod += low << N */
239 prodh
+= HHALF(low
) + (prodl
< was
);
241 if ((prodl
+= low
) < low
)
244 /* return 4N-bit product */