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