158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This software was developed by the Computer Systems Engineering group 658f0484fSRodney W. Grimes * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 758f0484fSRodney W. Grimes * contributed to Berkeley. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1858f0484fSRodney W. Grimes * must display the following acknowledgement: 1958f0484fSRodney W. Grimes * This product includes software developed by the University of 2058f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2158f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2258f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2358f0484fSRodney W. Grimes * without specific prior written permission. 2458f0484fSRodney W. Grimes * 2558f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2658f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2758f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2858f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2958f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3058f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3158f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3258f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3358f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3458f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3558f0484fSRodney W. Grimes * SUCH DAMAGE. 3658f0484fSRodney W. Grimes */ 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3958f0484fSRodney W. Grimes static char sccsid[] = "@(#)muldi3.c 8.1 (Berkeley) 6/4/93"; 4058f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 418fb3f3f6SDavid E. O'Brien #include <sys/cdefs.h> 428fb3f3f6SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes #include "quad.h" 4558f0484fSRodney W. Grimes 4658f0484fSRodney W. Grimes /* 4758f0484fSRodney W. Grimes * Multiply two quads. 4858f0484fSRodney W. Grimes * 4958f0484fSRodney W. Grimes * Our algorithm is based on the following. Split incoming quad values 5058f0484fSRodney W. Grimes * u and v (where u,v >= 0) into 5158f0484fSRodney W. Grimes * 5258f0484fSRodney W. Grimes * u = 2^n u1 * u0 (n = number of bits in `u_long', usu. 32) 5358f0484fSRodney W. Grimes * 5458f0484fSRodney W. Grimes * and 5558f0484fSRodney W. Grimes * 5658f0484fSRodney W. Grimes * v = 2^n v1 * v0 5758f0484fSRodney W. Grimes * 5858f0484fSRodney W. Grimes * Then 5958f0484fSRodney W. Grimes * 6058f0484fSRodney W. Grimes * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0 6158f0484fSRodney W. Grimes * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0 6258f0484fSRodney W. Grimes * 6358f0484fSRodney W. Grimes * Now add 2^n u1 v1 to the first term and subtract it from the middle, 6458f0484fSRodney W. Grimes * and add 2^n u0 v0 to the last term and subtract it from the middle. 6558f0484fSRodney W. Grimes * This gives: 6658f0484fSRodney W. Grimes * 6758f0484fSRodney W. Grimes * uv = (2^2n + 2^n) (u1 v1) + 6858f0484fSRodney W. Grimes * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) + 6958f0484fSRodney W. Grimes * (2^n + 1) (u0 v0) 7058f0484fSRodney W. Grimes * 7158f0484fSRodney W. Grimes * Factoring the middle a bit gives us: 7258f0484fSRodney W. Grimes * 7358f0484fSRodney W. Grimes * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high] 7458f0484fSRodney W. Grimes * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid] 7558f0484fSRodney W. Grimes * (2^n + 1) (u0 v0) [u0v0 = low] 7658f0484fSRodney W. Grimes * 7758f0484fSRodney W. Grimes * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done 7858f0484fSRodney W. Grimes * in just half the precision of the original. (Note that either or both 7958f0484fSRodney W. Grimes * of (u1 - u0) or (v0 - v1) may be negative.) 8058f0484fSRodney W. Grimes * 8158f0484fSRodney W. Grimes * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278. 8258f0484fSRodney W. Grimes * 8358f0484fSRodney W. Grimes * Since C does not give us a `long * long = quad' operator, we split 8458f0484fSRodney W. Grimes * our input quads into two longs, then split the two longs into two 8558f0484fSRodney W. Grimes * shorts. We can then calculate `short * short = long' in native 8658f0484fSRodney W. Grimes * arithmetic. 8758f0484fSRodney W. Grimes * 8858f0484fSRodney W. Grimes * Our product should, strictly speaking, be a `long quad', with 128 8958f0484fSRodney W. Grimes * bits, but we are going to discard the upper 64. In other words, 9058f0484fSRodney W. Grimes * we are not interested in uv, but rather in (uv mod 2^2n). This 9158f0484fSRodney W. Grimes * makes some of the terms above vanish, and we get: 9258f0484fSRodney W. Grimes * 9358f0484fSRodney W. Grimes * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low) 9458f0484fSRodney W. Grimes * 9558f0484fSRodney W. Grimes * or 9658f0484fSRodney W. Grimes * 9758f0484fSRodney W. Grimes * (2^n)(high + mid + low) + low 9858f0484fSRodney W. Grimes * 9958f0484fSRodney W. Grimes * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor 10058f0484fSRodney W. Grimes * of 2^n in either one will also vanish. Only `low' need be computed 10158f0484fSRodney W. Grimes * mod 2^2n, and only because of the final term above. 10258f0484fSRodney W. Grimes */ 10358f0484fSRodney W. Grimes static quad_t __lmulq(u_long, u_long); 10458f0484fSRodney W. Grimes 10558f0484fSRodney W. Grimes quad_t 10658f0484fSRodney W. Grimes __muldi3(a, b) 10758f0484fSRodney W. Grimes quad_t a, b; 10858f0484fSRodney W. Grimes { 10958f0484fSRodney W. Grimes union uu u, v, low, prod; 1108fb3f3f6SDavid E. O'Brien u_long high, mid, udiff, vdiff; 1118fb3f3f6SDavid E. O'Brien int negall, negmid; 11258f0484fSRodney W. Grimes #define u1 u.ul[H] 11358f0484fSRodney W. Grimes #define u0 u.ul[L] 11458f0484fSRodney W. Grimes #define v1 v.ul[H] 11558f0484fSRodney W. Grimes #define v0 v.ul[L] 11658f0484fSRodney W. Grimes 11758f0484fSRodney W. Grimes /* 11858f0484fSRodney W. Grimes * Get u and v such that u, v >= 0. When this is finished, 11958f0484fSRodney W. Grimes * u1, u0, v1, and v0 will be directly accessible through the 12058f0484fSRodney W. Grimes * longword fields. 12158f0484fSRodney W. Grimes */ 12258f0484fSRodney W. Grimes if (a >= 0) 12358f0484fSRodney W. Grimes u.q = a, negall = 0; 12458f0484fSRodney W. Grimes else 12558f0484fSRodney W. Grimes u.q = -a, negall = 1; 12658f0484fSRodney W. Grimes if (b >= 0) 12758f0484fSRodney W. Grimes v.q = b; 12858f0484fSRodney W. Grimes else 12958f0484fSRodney W. Grimes v.q = -b, negall ^= 1; 13058f0484fSRodney W. Grimes 13158f0484fSRodney W. Grimes if (u1 == 0 && v1 == 0) { 13258f0484fSRodney W. Grimes /* 13358f0484fSRodney W. Grimes * An (I hope) important optimization occurs when u1 and v1 13458f0484fSRodney W. Grimes * are both 0. This should be common since most numbers 13558f0484fSRodney W. Grimes * are small. Here the product is just u0*v0. 13658f0484fSRodney W. Grimes */ 13758f0484fSRodney W. Grimes prod.q = __lmulq(u0, v0); 13858f0484fSRodney W. Grimes } else { 13958f0484fSRodney W. Grimes /* 14058f0484fSRodney W. Grimes * Compute the three intermediate products, remembering 14158f0484fSRodney W. Grimes * whether the middle term is negative. We can discard 14258f0484fSRodney W. Grimes * any upper bits in high and mid, so we can use native 14358f0484fSRodney W. Grimes * u_long * u_long => u_long arithmetic. 14458f0484fSRodney W. Grimes */ 14558f0484fSRodney W. Grimes low.q = __lmulq(u0, v0); 14658f0484fSRodney W. Grimes 14758f0484fSRodney W. Grimes if (u1 >= u0) 14858f0484fSRodney W. Grimes negmid = 0, udiff = u1 - u0; 14958f0484fSRodney W. Grimes else 15058f0484fSRodney W. Grimes negmid = 1, udiff = u0 - u1; 15158f0484fSRodney W. Grimes if (v0 >= v1) 15258f0484fSRodney W. Grimes vdiff = v0 - v1; 15358f0484fSRodney W. Grimes else 15458f0484fSRodney W. Grimes vdiff = v1 - v0, negmid ^= 1; 15558f0484fSRodney W. Grimes mid = udiff * vdiff; 15658f0484fSRodney W. Grimes 15758f0484fSRodney W. Grimes high = u1 * v1; 15858f0484fSRodney W. Grimes 15958f0484fSRodney W. Grimes /* 16058f0484fSRodney W. Grimes * Assemble the final product. 16158f0484fSRodney W. Grimes */ 16258f0484fSRodney W. Grimes prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] + 16358f0484fSRodney W. Grimes low.ul[H]; 16458f0484fSRodney W. Grimes prod.ul[L] = low.ul[L]; 16558f0484fSRodney W. Grimes } 16658f0484fSRodney W. Grimes return (negall ? -prod.q : prod.q); 16758f0484fSRodney W. Grimes #undef u1 16858f0484fSRodney W. Grimes #undef u0 16958f0484fSRodney W. Grimes #undef v1 17058f0484fSRodney W. Grimes #undef v0 17158f0484fSRodney W. Grimes } 17258f0484fSRodney W. Grimes 17358f0484fSRodney W. Grimes /* 17458f0484fSRodney W. Grimes * Multiply two 2N-bit longs to produce a 4N-bit quad, where N is half 17558f0484fSRodney W. Grimes * the number of bits in a long (whatever that is---the code below 17658f0484fSRodney W. Grimes * does not care as long as quad.h does its part of the bargain---but 17758f0484fSRodney W. Grimes * typically N==16). 17858f0484fSRodney W. Grimes * 17958f0484fSRodney W. Grimes * We use the same algorithm from Knuth, but this time the modulo refinement 18058f0484fSRodney W. Grimes * does not apply. On the other hand, since N is half the size of a long, 18158f0484fSRodney W. Grimes * we can get away with native multiplication---none of our input terms 18258f0484fSRodney W. Grimes * exceeds (ULONG_MAX >> 1). 18358f0484fSRodney W. Grimes * 18458f0484fSRodney W. Grimes * Note that, for u_long l, the quad-precision result 18558f0484fSRodney W. Grimes * 18658f0484fSRodney W. Grimes * l << N 18758f0484fSRodney W. Grimes * 18858f0484fSRodney W. Grimes * splits into high and low longs as HHALF(l) and LHUP(l) respectively. 18958f0484fSRodney W. Grimes */ 19058f0484fSRodney W. Grimes static quad_t 19158f0484fSRodney W. Grimes __lmulq(u_long u, u_long v) 19258f0484fSRodney W. Grimes { 19358f0484fSRodney W. Grimes u_long u1, u0, v1, v0, udiff, vdiff, high, mid, low; 19458f0484fSRodney W. Grimes u_long prodh, prodl, was; 19558f0484fSRodney W. Grimes union uu prod; 19658f0484fSRodney W. Grimes int neg; 19758f0484fSRodney W. Grimes 19858f0484fSRodney W. Grimes u1 = HHALF(u); 19958f0484fSRodney W. Grimes u0 = LHALF(u); 20058f0484fSRodney W. Grimes v1 = HHALF(v); 20158f0484fSRodney W. Grimes v0 = LHALF(v); 20258f0484fSRodney W. Grimes 20358f0484fSRodney W. Grimes low = u0 * v0; 20458f0484fSRodney W. Grimes 20558f0484fSRodney W. Grimes /* This is the same small-number optimization as before. */ 20658f0484fSRodney W. Grimes if (u1 == 0 && v1 == 0) 20758f0484fSRodney W. Grimes return (low); 20858f0484fSRodney W. Grimes 20958f0484fSRodney W. Grimes if (u1 >= u0) 21058f0484fSRodney W. Grimes udiff = u1 - u0, neg = 0; 21158f0484fSRodney W. Grimes else 21258f0484fSRodney W. Grimes udiff = u0 - u1, neg = 1; 21358f0484fSRodney W. Grimes if (v0 >= v1) 21458f0484fSRodney W. Grimes vdiff = v0 - v1; 21558f0484fSRodney W. Grimes else 21658f0484fSRodney W. Grimes vdiff = v1 - v0, neg ^= 1; 21758f0484fSRodney W. Grimes mid = udiff * vdiff; 21858f0484fSRodney W. Grimes 21958f0484fSRodney W. Grimes high = u1 * v1; 22058f0484fSRodney W. Grimes 22158f0484fSRodney W. Grimes /* prod = (high << 2N) + (high << N); */ 22258f0484fSRodney W. Grimes prodh = high + HHALF(high); 22358f0484fSRodney W. Grimes prodl = LHUP(high); 22458f0484fSRodney W. Grimes 22558f0484fSRodney W. Grimes /* if (neg) prod -= mid << N; else prod += mid << N; */ 22658f0484fSRodney W. Grimes if (neg) { 22758f0484fSRodney W. Grimes was = prodl; 22858f0484fSRodney W. Grimes prodl -= LHUP(mid); 22958f0484fSRodney W. Grimes prodh -= HHALF(mid) + (prodl > was); 23058f0484fSRodney W. Grimes } else { 23158f0484fSRodney W. Grimes was = prodl; 23258f0484fSRodney W. Grimes prodl += LHUP(mid); 23358f0484fSRodney W. Grimes prodh += HHALF(mid) + (prodl < was); 23458f0484fSRodney W. Grimes } 23558f0484fSRodney W. Grimes 23658f0484fSRodney W. Grimes /* prod += low << N */ 23758f0484fSRodney W. Grimes was = prodl; 23858f0484fSRodney W. Grimes prodl += LHUP(low); 23958f0484fSRodney W. Grimes prodh += HHALF(low) + (prodl < was); 24058f0484fSRodney W. Grimes /* ... + low; */ 24158f0484fSRodney W. Grimes if ((prodl += low) < low) 24258f0484fSRodney W. Grimes prodh++; 24358f0484fSRodney W. Grimes 24458f0484fSRodney W. Grimes /* return 4N-bit product */ 24558f0484fSRodney W. Grimes prod.ul[H] = prodh; 24658f0484fSRodney W. Grimes prod.ul[L] = prodl; 24758f0484fSRodney W. Grimes return (prod.q); 24858f0484fSRodney W. Grimes } 249