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