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