muldi3.c revision 5ce758aedffaa9134685f699eb3d4255274038b6
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync/* $NetBSD: muldi3.c,v 1.10 2005/12/11 12:24:37 christos Exp $ */
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync/*-
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Copyright (c) 1992, 1993
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * The Regents of the University of California. All rights reserved.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This software was developed by the Computer Systems Engineering group
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * contributed to Berkeley.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Redistribution and use in source and binary forms, with or without
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * modification, are permitted provided that the following conditions
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * are met:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 1. Redistributions of source code must retain the above copyright
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * notice, this list of conditions and the following disclaimer.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 2. Redistributions in binary form must reproduce the above copyright
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * notice, this list of conditions and the following disclaimer in the
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * documentation and/or other materials provided with the distribution.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * 3. Neither the name of the University nor the names of its contributors
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * may be used to endorse or promote products derived from this software
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * without specific prior written permission.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * SUCH DAMAGE.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync */
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync/*#include <sys/cdefs.h>
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#if defined(LIBC_SCCS) && !defined(lint)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#if 0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsyncstatic char sccsid[] = "@(#)muldi3.c 8.1 (Berkeley) 6/4/93";
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#else
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync__RCSID("$NetBSD: muldi3.c,v 1.10 2005/12/11 12:24:37 christos Exp $");
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#endif
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#endif*/ /* LIBC_SCCS and not lint */
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync#include "quad.h"
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync/*
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Multiply two quads.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Our algorithm is based on the following. Split incoming quad values
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * u and v (where u,v >= 0) into
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * u = 2^n u1 * u0 (n = number of bits in `u_int', usu. 32)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * and
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * v = 2^n v1 * v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Then
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = 2^2n u1 v1 + 2^n u1 v0 + 2^n v1 u0 + u0 v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * = 2^2n u1 v1 + 2^n (u1 v0 + v1 u0) + u0 v0
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Now add 2^n u1 v1 to the first term and subtract it from the middle,
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * and add 2^n u0 v0 to the last term and subtract it from the middle.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This gives:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = (2^2n + 2^n) (u1 v1) +
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n) (u1 v0 - u1 v1 + u0 v1 - u0 v0) +
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n + 1) (u0 v0)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Factoring the middle a bit gives us:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * uv = (2^2n + 2^n) (u1 v1) + [u1v1 = high]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n) (u1 - u0) (v0 - v1) + [(u1-u0)... = mid]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n + 1) (u0 v0) [u0v0 = low]
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * The terms (u1 v1), (u1 - u0) (v0 - v1), and (u0 v0) can all be done
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * in just half the precision of the original. (Note that either or both
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * of (u1 - u0) or (v0 - v1) may be negative.)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * This algorithm is from Knuth vol. 2 (2nd ed), section 4.3.3, p. 278.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Since C does not give us a `int * int = quad' operator, we split
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * our input quads into two ints, then split the two ints into two
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * shorts. We can then calculate `short * short = int' in native
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * arithmetic.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Our product should, strictly speaking, be a `long quad', with 128
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * bits, but we are going to discard the upper 64. In other words,
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * we are not interested in uv, but rather in (uv mod 2^2n). This
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * makes some of the terms above vanish, and we get:
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n)(high) + (2^n)(mid) + (2^n + 1)(low)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * or
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * (2^n)(high + mid + low) + low
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync *
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * Furthermore, `high' and `mid' can be computed mod 2^n, as any factor
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * of 2^n in either one will also vanish. Only `low' need be computed
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync * mod 2^2n, and only because of the final term above.
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync */
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsyncstatic quad_t __lmulq(u_int, u_int);
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsyncquad_t
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync__muldi3(a, b)
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync quad_t a, b;
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync{
61cb83a8ccd1dd7f671f31fa93c9d8b7be09b4ccvboxsync union uu u, v, low, prod;
u_int high, mid, udiff, vdiff;
int negall, negmid;
#define u1 u.ul[H]
#define u0 u.ul[L]
#define v1 v.ul[H]
#define v0 v.ul[L]
/*
* Get u and v such that u, v >= 0. When this is finished,
* u1, u0, v1, and v0 will be directly accessible through the
* int fields.
*/
if (a >= 0)
u.q = a, negall = 0;
else
u.q = -a, negall = 1;
if (b >= 0)
v.q = b;
else
v.q = -b, negall ^= 1;
if (u1 == 0 && v1 == 0) {
/*
* An (I hope) important optimization occurs when u1 and v1
* are both 0. This should be common since most numbers
* are small. Here the product is just u0*v0.
*/
prod.q = __lmulq(u0, v0);
} else {
/*
* Compute the three intermediate products, remembering
* whether the middle term is negative. We can discard
* any upper bits in high and mid, so we can use native
* u_int * u_int => u_int arithmetic.
*/
low.q = __lmulq(u0, v0);
if (u1 >= u0)
negmid = 0, udiff = u1 - u0;
else
negmid = 1, udiff = u0 - u1;
if (v0 >= v1)
vdiff = v0 - v1;
else
vdiff = v1 - v0, negmid ^= 1;
mid = udiff * vdiff;
high = u1 * v1;
/*
* Assemble the final product.
*/
prod.ul[H] = high + (negmid ? -mid : mid) + low.ul[L] +
low.ul[H];
prod.ul[L] = low.ul[L];
}
return (negall ? -prod.q : prod.q);
#undef u1
#undef u0
#undef v1
#undef v0
}
/*
* Multiply two 2N-bit ints to produce a 4N-bit quad, where N is half
* the number of bits in an int (whatever that is---the code below
* does not care as long as quad.h does its part of the bargain---but
* typically N==16).
*
* We use the same algorithm from Knuth, but this time the modulo refinement
* does not apply. On the other hand, since N is half the size of an int,
* we can get away with native multiplication---none of our input terms
* exceeds (UINT_MAX >> 1).
*
* Note that, for u_int l, the quad-precision result
*
* l << N
*
* splits into high and low ints as HHALF(l) and LHUP(l) respectively.
*/
static quad_t
__lmulq(u_int u, u_int v)
{
u_int u1, u0, v1, v0, udiff, vdiff, high, mid, low;
u_int prodh, prodl, was;
union uu prod;
int neg;
u1 = HHALF(u);
u0 = LHALF(u);
v1 = HHALF(v);
v0 = LHALF(v);
low = u0 * v0;
/* This is the same small-number optimization as before. */
if (u1 == 0 && v1 == 0)
return (low);
if (u1 >= u0)
udiff = u1 - u0, neg = 0;
else
udiff = u0 - u1, neg = 1;
if (v0 >= v1)
vdiff = v0 - v1;
else
vdiff = v1 - v0, neg ^= 1;
mid = udiff * vdiff;
high = u1 * v1;
/* prod = (high << 2N) + (high << N); */
prodh = high + HHALF(high);
prodl = LHUP(high);
/* if (neg) prod -= mid << N; else prod += mid << N; */
if (neg) {
was = prodl;
prodl -= LHUP(mid);
prodh -= HHALF(mid) + (prodl > was);
} else {
was = prodl;
prodl += LHUP(mid);
prodh += HHALF(mid) + (prodl < was);
}
/* prod += low << N */
was = prodl;
prodl += LHUP(low);
prodh += HHALF(low) + (prodl < was);
/* ... + low; */
if ((prodl += low) < low)
prodh++;
/* return 4N-bit product */
prod.ul[H] = prodh;
prod.ul[L] = prodl;
return (prod.q);
}